Skip to content

Instantly share code, notes, and snippets.

@jphase
Created March 15, 2014 04:09
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 jphase/9561722 to your computer and use it in GitHub Desktop.
Save jphase/9561722 to your computer and use it in GitHub Desktop.
Force numeric input with cmd and ctrl actions
// Force numeric only input
$.fn.numeric = function() {
return this.each(function() {
$(this).keydown(function(e) {
var key = e.charCode || e.keyCode || 0;
// Allow backspace, tab, delete, arrows, numbers, keypad numbers, home, end, period, cmd + a, cmd + r, ctrl + a, ctrl + r, and numpad decimal ONLY
return (key == 8 || key == 9 || key == 46 || key == 110 || key == 190 || (key >= 35 && key <= 40) || (key >= 48 && key <= 57) || (key >= 96 && key <= 105) || (key == 65 && e.ctrlKey === true) || (key == 82 && e.ctrlKey === true) || (key == 65 && e.metaKey === true) || (key == 82 && e.metaKey === true));
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment