-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjquery-numeric.js
34 lines (29 loc) · 1.05 KB
/
jquery-numeric.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
/*!
* Numeric.js Jquery extension v0.1.0
* https://github.com/lcavadas/jquery-numeric
*
* Copyright 2012, Luís Serralheiro
*/
(function ($) {
$.fn.numeric = function (opt) {
var that = this;
var opts = {max: Number(this.attr('max')), min: Number(this.attr('min')), decimal: true};
$.extend(opts, opt);
var currentNumber;
this.bind('keypress paste drop', function (e) {
currentNumber = that.val();
var validate = function (numberToValidate, defaultNumber) {
if (!isNaN(numberToValidate) && numberToValidate <= opts.max && numberToValidate >= opts.min) {
that.val(opts.decimal ? numberToValidate : Number(numberToValidate).toFixed(0));
} else {
if (that.val() !== '-') {
that.val(defaultNumber);
}
}
};
window.setTimeout(function () {
validate(that.val(), currentNumber);
}, 1);
});
};
}(jQuery));