Skip to content

Instantly share code, notes, and snippets.

@mosinski
Last active August 29, 2015 14:15
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 mosinski/9d3019facbd371aa18f3 to your computer and use it in GitHub Desktop.
Save mosinski/9d3019facbd371aa18f3 to your computer and use it in GitHub Desktop.
input allow only numbers
$(function() {
$('input[type=number]').keydown(function (e) {
var key = e.which || e.keyCode;
if (!e.shiftKey && !e.altKey && !e.ctrlKey &&
// numbers
key >= 48 && key <= 57 ||
// Numeric keypad
key >= 96 && key <= 105 ||
// comma, period and minus, . on keypad
key == 190 || key == 110 ||
// Backspace and Tab and Enter
key == 8 || key == 9 || key == 13 ||
// Home and End
key == 35 || key == 36 ||
// left and right arrows
key == 37 || key == 39 ||
// Del and Ins
key == 46 || key == 45)
return true;
return false;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment