Skip to content

Instantly share code, notes, and snippets.

@wholypantalones
Created July 10, 2012 13:52
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 wholypantalones/3083362 to your computer and use it in GitHub Desktop.
Save wholypantalones/3083362 to your computer and use it in GitHub Desktop.
jQuery ForceNumericOnly Plugin
jQuery.fn.ForceNumericOnly =
function()
{
return this.each(function()
{
$(this).keydown(function(e)
{
var key = e.charCode || e.keyCode || 0;
// allow backspace, tab, delete, arrows, numbers and keypad numbers ONLY
return (
key == 8 ||
key == 9 ||
key == 46 ||
(key >= 37 && key <= 40) ||
(key >= 48 && key <= 57) ||
(key >= 96 && key <= 105));
})
})
};
//usage
$("#yourTextBoxName").ForceNumericOnly();
@igorjacauna
Copy link

The Enter key should be allowed too.

@pranavcp
Copy link

this is allowing ( and ) also

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment