-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery.samask-masker.js
76 lines (64 loc) · 1.96 KB
/
jquery.samask-masker.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? factory() :
typeof define === 'function' && define.amd ? define(factory) :
(factory());
}(this, (function () { 'use strict';
function Samask(target) {
var maskText = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var self;
var regexLocalTable = {
'0': {
pattern: /\d/
},
'S': {
pattern: /[a-zA-Z]/
}
};
var getRegex = function getRegex(position) {
return regexLocalTable[maskText[position]];
};
var isSplitter = function isSplitter(char, position) {
return char == maskText[position];
};
var regexTest = function regexTest(char, position) {
var regex = getRegex(position);
if (regex && regex.pattern.test(char)) {
return true;
} else if (isSplitter(char, position)) {
return true;
} else if (getRegex(position + 1) && getRegex(position + 1).pattern.test(char)) {
self.value += maskText[position] + char;
return false;
} else {
return false;
}
};
var mask = function mask() {
self = this;
this.setAttribute('maxlength', maskText.length);
this.addEventListener('keypress', function (e) {
var char = e.key;
var position = this.value.length;
if (!regexTest(char, position)) {
e.preventDefault();
}
});
};
target.each(mask);
}
if (typeof window !== 'undefined') {
var plugin = window.$ || window.jQuery || window.Zepto;
if (plugin) {
plugin.fn.samask = function SamaskPlugin(maskText) {
Samask(this, maskText);
return this;
};
plugin.samaskHtml = function SamaskHtmlPlugin() {
var domElements = $('input');
domElements.filter("*[data-samask]").each(function () {
Samask($(this), this.getAttribute('data-samask'));
});
};
}
}
})));