Reputation: 43
I made jQuery function that allows only numbers and one point in input type=text
.
It works in IE and Chrome, but doesn't in Firefox. Can anyone help?
Here is the code:
$(document).ready(function () {
$(".inputbox").keypress(function (e) {
//Allow only one point
if (String.fromCharCode(e.keyCode).match("[.]") && this.value.indexOf(".") != -1) {
return false;
}
//Allow just numbers
if (String.fromCharCode(e.keyCode).match(/[^0-9.]/g)) {
return false;
}
return true;
});
});
Upvotes: 1
Views: 148
Reputation: 382122
The problem isn't in the regular expression but in e.keyCode
. Some browsers use keyCode
, others use e.which
.
jQuery handles this browser difference for you, making sure that e.which
works cross-browser.
Upvotes: 8