diff --git a/numeric/jquery.numeric.js b/numeric/jquery.numeric.js
index d07520f..3f90d5c 100644
--- a/numeric/jquery.numeric.js
+++ b/numeric/jquery.numeric.js
@@ -21,6 +21,8 @@
* @example $(".numeric").numeric();
* @example $(".numeric").numeric(","); // use , as separator
* @example $(".numeric").numeric({ decimal : "," }); // use , as separator
+ * @example $(".numeric").numeric({ max : 999 }); // max value
+ * @example $(".numeric").numeric({ max : 'auto' }); // max value
* @example $(".numeric").numeric({ negative : false }); // do not allow negative values
* @example $(".numeric").numeric(null, callback); // use default values, pass on the 'callback' function
*
@@ -34,14 +36,17 @@ $.fn.numeric = function(config, callback)
config = config || {};
// if config.negative undefined, set to true (default is to allow negative numbers)
if(typeof config.negative == "undefined") { config.negative = true; }
+ if(typeof config.max == "undefined") { config.max = false; }
// set decimal point
var decimal = (config.decimal === false) ? "" : config.decimal || ".";
// allow negatives
var negative = (config.negative === true) ? true : false;
+ // max value
+ var max = (config.max === false) ? false : config.max;
// callback function
callback = (typeof(callback) == "function" ? callback : function() {});
// set data and methods
- return this.data("numeric.decimal", decimal).data("numeric.negative", negative).data("numeric.callback", callback).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur);
+ return this.data("numeric.decimal", decimal).data("numeric.negative", negative).data("numeric.max", max).data("numeric.callback", callback).keypress($.fn.numeric.keypress).keyup($.fn.numeric.keyup).blur($.fn.numeric.blur);
};
$.fn.numeric.keypress = function(e)
@@ -134,12 +139,16 @@ $.fn.numeric.keypress = function(e)
{
allow = true;
}
+ if(allow)
+ {
+ allow = $.fn.checkMaxValue(this);
+ }
return allow;
};
$.fn.numeric.keyup = function(e)
{
- var val = $(this).value;
+ var val = this.value;
if(val && val.length > 0)
{
// get carat (cursor) position
@@ -217,6 +226,7 @@ $.fn.numeric.keyup = function(e)
}
// set the value and prevent the cursor moving to the end
this.value = val;
+ $.fn.checkMaxValue(this);
$.fn.setSelection(this, carat);
}
};
@@ -277,4 +287,21 @@ $.fn.setSelection = function(o, p)
}
};
+// check max value
+$.fn.checkMaxValue = function(e)
+{
+ var max = $.data(e, "numeric.max");
+ if(max == 'auto')
+ {
+ max = $(e).data("max");
+ }
+ var value = $(e).val();
+ if(max && value >= max)
+ {
+ $(e).val(max);
+ return false;
+ }
+ return true;
+};
+
})(jQuery);
diff --git a/numeric/test.html b/numeric/test.html
index 631dbfc..5ff0d0f 100644
--- a/numeric/test.html
+++ b/numeric/test.html
@@ -10,6 +10,10 @@
Integers only:
+ Integers only, max 999:
+
+ Integers only, max auto:
+
No negative values:
No negative values (integer only):
@@ -19,6 +23,8 @@