by Vitaliy Yanchuk Key filter Description Function to filter character

 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
/*
by Vitaliy Yanchuk
Key filter:
Description: Function to filter characters from input field
Usage: $("#inputtext").key_filter('[a-z0-9]', 'i');
*/
(function($){
function key_filter(p, flags, option){
var reg = new RegExp(p, flags);
$(this).keypress( function(e){
// Keys will pass:
if(e.keyCode == 8 || // Backspace
e.keyCode == 9 || // Tab
e.keyCode == 33 || // PageUP
e.keyCode == 34 || // PageDown
e.keyCode == 35 || // End
e.keyCode == 36 || // Home
e.keyCode == 37 || // LeftArrow
e.keyCode == 39 || // RightArrow
e.keyCode == 45 || // Insert
e.keyCode == 46){ // Delete
return true;
}else{
if( !reg.test( String.fromCharCode(e.which) )){ return false; }
}
});
}
$.fn.extend({
'key_filter' : key_filter ,
});
})(jQuery);