on('keydown keypress',function (e){});
First, triggers twice on iOS (no clue why), and bind
fails on FireFox, so just use $().keypress
.
Your filtering was letting through the right keycodes for the numbers you wanted, but, not catching the characters you needed to catch, at first I had a solution going which used e.orginialEvent.keyIdentifier to go after the misbehaving keys, but this failed dramatically on firefox.
In looking for a solution to that problem I found and modified code from this page about keycodes and charcodes in Firefox.
$(formSelector).keypress(function (e) {
var k = e.keyCode || e.charCode;
var c = e.charCode;
var isArrow = (c == 0 && k >= 37 && k <= 40);
var isSpecial = ((k == 8) || (k == 9) || (k == 127)) || isArrow; // backspace, tab, delete
var isOK = (k >= 48 && k <= 57) ; // numbers
return isOK || isSpecial;
});
Live Versions: