Skip to content

Instantly share code, notes, and snippets.

@Chavao
Created May 10, 2012 18:29
Show Gist options
  • Save Chavao/2654905 to your computer and use it in GitHub Desktop.
Save Chavao/2654905 to your computer and use it in GitHub Desktop.
Force numeric only
jQuery.fn.ForceNumericOnly = function() {
return this.each(function()
{
$(this).keydown(function(e)
{
var key = e.charCode || e.keyCode || 0;
// allow backspace, tab, delete, arrows, ctrl + a, numbers and keypad numbers ONLY
return (
key == 8 ||
key == 9 ||
key == 46 ||
(key >= 37 && key <= 40) ||
(key >= 48 && key <= 57) ||
(key == 65 && e.ctrlKey === true) ||
(key >= 96 && key <= 105));
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment