-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.validateInput.js
More file actions
82 lines (77 loc) · 2.46 KB
/
jquery.validateInput.js
File metadata and controls
82 lines (77 loc) · 2.46 KB
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
82
;
(function ($) {
$.extend($.fn,
{
validateNumbersInput: function () {
var ctrlDown = false;
var cmdDownLeft = false;
var cmdDownRight = false;
var cmdLeftCode = 91;
var cmdRightCode = 93;
this.bind(
{
keydown: function (e) {
var charCode = (e.which) ? e.which : e.keyCode;
if (charCode == cmdLeftCode) {
cmdDownLeft = true;
return true;
}
if (charCode == cmdRightCode) {
cmdDownRight = true;
return true;
}
if (charCode == 17) {
ctrlDown = true;
return true;
}
var isDecimalPoint = charCode == 190 || charCode == 110;
if (isDecimalPoint) {
return $(this).val().indexOf(".") == -1;
}
var isMetaKeyPressed = ctrlDown || cmdDownLeft || cmdDownRight;
if (charCode == 0 && isMetaKeyPressed) // bag in mac safari
return true;
var isModificationChar = charCode == 86 || charCode == 88 || charCode == 90 || charCode == 67;
return (charCode > 32 && charCode < 57) || // navigation + main numbers
(charCode > 95 && charCode < 106) || // right numbers
charCode == 8 || // backspace
(isModificationChar && isMetaKeyPressed); // ctrl + c (copy)
// more codes are here http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes
},
keyup: function (e) {
var charCode = (e.which) ? e.which : e.keyCode;
if (charCode == cmdLeftCode) {
cmdDownLeft = false;
}
if (charCode == cmdRightCode) {
cmdDownRight = false;
}
if (charCode == 17) {
ctrlDown = false;
}
},
paste: function (e) {
var clipBoard;
try
{
clipBoard = e.originalEvent.clipboardData.getData('Text')
}
catch(exeption)
{
try
{
clipBoard = clipboardData.getData("text");
}
catch (subexeption) {
}
}
if (!clipBoard) {
return true;
}
return !isNaN(clipBoard);
}
});
}
});
})
(jQuery);