-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathjquery.alphanumeric.js
executable file
·81 lines (65 loc) · 2.41 KB
/
jquery.alphanumeric.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
77
78
79
80
81
(function ($) {
$.fn.alphanumeric = function (p) {
var input = $(this),
//add new letter (ñ, for spanish keyboard) (Felipe Pincheira A.)
// + french letter
az = 'aàáâãäåæbcçdeèéêëfghiìíîïjklmnñoðòóôõöpqrstuùúûüvwxyýÿz',
options = $.extend({
//add new characters (¡°¬´¨) (Felipe Pincheira A.)
ichars: '!@#$%^&*()+=[]\\\';,/{}|":<>?~`.- _¡°¬´¨²€ƒ^‰¥£¢¦§¨ø',
nchars: '',
allow: ''
}, p),
s = options.allow.split(''),
i = 0,
ch,
regex;
for (i; i < s.length; i++) {
if (options.ichars.indexOf(s[i]) != -1) {
s[i] = '\\' + s[i];
}
}
if (options.nocaps) {
options.nchars += az.toUpperCase();
}
if (options.allcaps) {
options.nchars += az;
}
options.allow = s.join('|');
regex = new RegExp(options.allow, 'gi');
ch = (options.ichars + options.nchars).replace(regex, '');
input
.keypress(function (e) {
var key = String.fromCharCode(!e.charCode ? e.which : e.charCode);
if (ch.indexOf(key) != -1 && !e.ctrlKey) {
e.preventDefault();
}
})
.blur(function () {
var value = input.val(),
j = 0;
for (j; j < value.length; j++) {
if (ch.indexOf(value[j]) != -1) {
input.val('');
return false;
}
}
return false;
});
return input;
};
$.fn.numeric = function (p) {
//add new letter (ñ, for spanish keyboard) (Felipe Pincheira A.)
var az = 'aàáâãäåæbcçdeèéêëfghiìíîïjklmnñoðòóôõöpqrstuùúûüvwxyýÿz',
aZ = az.toUpperCase();
return this.each(function () {
$(this).alphanumeric($.extend({ nchars: az + aZ }, p));
});
};
$.fn.alpha = function (p) {
var nm = '1234567890';
return this.each(function () {
$(this).alphanumeric($.extend({ nchars: nm }, p));
});
};
})(jQuery);