-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjquery.restrict.js
37 lines (26 loc) · 956 Bytes
/
jquery.restrict.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
(function($) {
$.fn.restrict = function(pattern, allowed) {
allowed = allowed || [];
$(this).keypress(function(event) {
if(event.which) {
// Get the pressed key
var key = event.which;
// Return true if in array of allowed characters
if($.inArray(key, allowed) > -1) {
return true;
}
// Get the characted form the pressed key
var character = String.fromCharCode(key);
// Test the character against the defined pattern
regEx = new RegExp(pattern);
if(regEx.test(character)) {
return true;
}
// If the test fails, prevent the key from being typed
event.preventDefault();
return false;
};
});
return this;
};
})(jQuery);