Skip to content

Instantly share code, notes, and snippets.

@cristobal
Created August 31, 2011 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cristobal/1183493 to your computer and use it in GitHub Desktop.
Save cristobal/1183493 to your computer and use it in GitHub Desktop.
jQuery Numeric - Number validator
/**
* Numeric.js
*
* @author Cristobal Dabed
* @version 0.1
*/
(function ($) {
var re = new RegExp('[A-Za-z\\]\\[!\\"#$%&\'()*+,.\\/:;<=>?@\\^_`{|}~-]');
var re_s = new RegExp("\\D");
$.fn.numeric = function(strict) {
return $(this).bind("keydown keypress keyup", function(event) {
$(this).val(
$(this).val().replace((strict ? re_s : re), "")
);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment