I have the following code and am trying to allow only 3 digits 0-9 in the input, but still allow the user to use the arrow keys, backspace, and delete keys.
It works in other browsers, but in firefox the output to the console shows the arrow keys and the delete key registering as key code 0. What gives?
Code:
$input.
keypress(function(e) {
var currentLength = $input.val().length;
var c = e.which;
console.log("key", c);
//0=48, 9=57, 37-40 = arrow keys 8= backspace, 46=delete
if (c === 8 || c === 46 || (c >= 37 && c <=40)) {
//delete, backspace, and arrow keys are allowed
} else if (c < 48 || c > 57 || currentLength >= 3) {
e.preventDefault();
}
}).on("paste", function(e) {
e.preventDefault();
});
Fiddle: http://jsfiddle.net/2cjpdfcy/