Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions numeric/jquery.numeric.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <input type="text" data-max="999" />
* @example $(".numeric").numeric({ negative : false }); // do not allow negative values
* @example $(".numeric").numeric(null, callback); // use default values, pass on the 'callback' function
*
Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
};
Expand Down Expand Up @@ -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);
6 changes: 6 additions & 0 deletions numeric/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<input class="numeric" type="text" />
Integers only:
<input class="integer" type="text" />
Integers only, max 999:
<input class="integer-max" type="text" />
Integers only, max auto:
<input class="integer-max-auto" type="text" data-max="999"/>
No negative values:
<input class="positive" type="text" />
No negative values (integer only):
Expand All @@ -19,6 +23,8 @@
<script type="text/javascript">
$(".numeric").numeric();
$(".integer").numeric(false, function() { alert("Integers only"); this.value = ""; this.focus(); });
$(".integer-max").numeric({ max: 999 });
$(".integer-max-auto").numeric({ max: 'auto' });
$(".positive").numeric({ negative: false }, function() { alert("No negative values"); this.value = ""; this.focus(); });
$(".positive-integer").numeric({ decimal: false, negative: false }, function() { alert("Positive integers only"); this.value = ""; this.focus(); });
$("#remove").click(
Expand Down