-
Notifications
You must be signed in to change notification settings - Fork 4
/
regex-mask-plugin.js
26 lines (26 loc) · 1016 Bytes
/
regex-mask-plugin.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
(function ($){
$.fn.regexMask = function (mask) {
if (!mask) {
throw 'mandatory mask argument missing';
} else if (mask == 'float-ptbr') {
mask = /^((\d{1,3}(\.\d{3})*(((\.\d{0,2}))|((\,\d*)?)))|(\d+(\,\d*)?))$/;
} else if (mask == 'float-enus') {
mask = /^((\d{1,3}(\,\d{3})*(((\,\d{0,2}))|((\.\d*)?)))|(\d+(\.\d*)?))$/;
} else if (mask == 'integer') {
mask = /^\d+$/;
} else {
try {
mask.test("");
} catch(e) {
throw 'mask regex need to support test method';
}
}
$(this).keypress(function (event) {
if (!event.charCode) return true;
var part1 = this.value.substring(0,this.selectionStart);
var part2 = this.value.substring(this.selectionEnd,this.value.length);
if (!mask.test(part1 + String.fromCharCode(event.charCode) + part2))
return false;
});
};
})(jQuery);