This repository has been archived by the owner on Jan 18, 2021. It is now read-only.
forked from joshuadeleon/NumericInput
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnumericInput.js
176 lines (146 loc) · 5.04 KB
/
numericInput.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
///////////////////////////////////////////////////////////////
// Author: Joshua De Leon
// File: numericInput.js
// Description: Allows only numeric input in an element.
//
// If you happen upon this code, enjoy it, learn from it, and
// if possible please credit me: www.transtatic.com
///////////////////////////////////////////////////////////////
/**
* Markenwerk fork of the original library containing decimal separator options
* @see https://github.com/markenwerk/NumericInput
*/
// Sets a keypress event for the selected element allowing only numbers. Typically this would only be bound to a textbox.
(function ($) {
// Plugin defaults
var defaults = {
allowFloat: false,
allowNegative: false,
min: undefined,
max: undefined,
decimalSeparator: 'PERIOD'
};
// Plugin definition
// allowFloat: (boolean) Allows floating point (real) numbers. If set to false only integers will be allowed. Default: false.
// allowNegative: (boolean) Allows negative values. If set to false only positive number input will be allowed. Default: false.
// min: (int/float) If set, when the user leaves the input if the entered value is too low it will be set to this value
// max: (int/float) If set, when the user leaves the input if the entered value is too high it will be set to this value
$.fn.numericInput = function (options) {
var settings = $.extend({}, defaults, options);
var allowFloat = settings.allowFloat;
var allowNegative = settings.allowNegative;
var min = settings.min;
var max = settings.max;
var decimalSeparator = settings.decimalSeparator;
if (min === max) {
throw("The minimum value cannot be the same as the max value");
}
// If the values are swapped we swap them back
else if (min > max) {
var temp = min;
min = max;
max = temp;
}
var deciamlPointCharCode = (decimalSeparator.toUpperCase() === 'COMMA') ? 44 : 46;
var deciamlPointPattern = (decimalSeparator.toUpperCase() === 'COMMA') ? /[,]/ : /[.]/;
var controlDown = false;
var controlKeyCodes = [
224, // Firefox
17, // Opera
91, // WebKit (Safari/Chrome) (Left Apple)
93 // WebKit (Safari/Chrome) (Right Apple)
];
this.keydown(function (event) {
if (event.metaKey || event.ctrlKey || jQuery.inArray(parseInt(event.which), controlKeyCodes) !== -1) {
controlDown = true;
}
});
this.keyup(function (event) {
if (event.metaKey || event.ctrlKey || jQuery.inArray(parseInt(event.which), controlKeyCodes) !== -1) {
controlDown = false;
}
});
this.keypress(function (event) {
if (controlDown) {
return true;
}
var inputCode = event.which;
var currentValue = $(this).val();
// Checks the if the character code is not a digit
if (inputCode > 0 && (inputCode < 48 || inputCode > 57)) {
// Conditions for a period (decimal point)
if (allowFloat === true && inputCode === deciamlPointCharCode) {
// Disallows a period before a negative
if (allowNegative === true && getCaret(this) === 0 && currentValue.charAt(0) === '-') {
return false;
}
// Disallows more than one decimal point.
if (currentValue.match(deciamlPointPattern)) {
return false;
}
}
// Conditions for a minus
else if (allowNegative === true && inputCode === 45) {
if (currentValue.charAt(0) === '-') {
return false;
}
if (getCaret(this) !== 0) {
return false;
}
}
// Allows backspace, ctrl+c, ctrl+v (copy & paste), enter (submit)
else if (inputCode === 8 || inputCode === 67 || inputCode === 86 || inputCode === 13) {
return true;
}
// Disallow non-numeric
else {
return false;
}
}
// Disallows numbers before a negative.
else if (inputCode > 0 && (inputCode >= 48 && inputCode <= 57)) {
if (allowNegative === true && currentValue.charAt(0) === '-' && getCaret(this) === 0) {
return false;
}
}
});
this.blur(function (event) {
//Get and store the current value
var currentValue = $(this).val();
//If the value isn't empty
if (currentValue.length > 0) {
//Get the float value, even if we're not using floats this will be ok
var floatValue = parseFloat(currentValue);
//If min is specified and the value is less set the value to min
if (min !== undefined && floatValue < min) {
$(this).val(min);
}
//If max is specified and the value is less set the value to max
if (max !== undefined && floatValue > max) {
$(this).val(max);
}
}
});
return this;
};
// Private function for selecting cursor position. Makes IE play nice.
// http://stackoverflow.com/questions/263743/how-to-get-caret-position-in-textarea
function getCaret(element) {
if (element.selectionStart) {
return element.selectionStart;
} else if (document.selection) //IE specific
{
element.focus();
var r = document.selection.createRange();
if (r == null) {
return 0;
}
var re = element.createTextRange(),
rc = re.duplicate();
re.moveToBookmark(r.getBookmark());
rc.setEndPoint('EndToStart', re);
return rc.text.length;
}
return 0;
}
}(jQuery));