Reputation: 33
this is my javascript function to prevent user not to enter alphabets and special characters in the textbox field, it works fine but it is not even accepting numeric.
please note that it is working fine Firefox.
function isNumber(event) {
var regex = new RegExp("^[0-9]+$");
var str = String.fromCharCode(!event.charCode ? event.which : event.charCode);
if (regex.test(str)) {
return true;
}
if (window.event) {
window.event.returnValue = false;
}
return false;
}
and this is my textbox
<asp:TextBox Width="50px" ID="placesvis" runat="server" onkeypress="isNumber(event);"
Upvotes: 0
Views: 52
Reputation: 28128
Why not simply set the input type to number?
<input type="number" id="quantity" name="quantity" min="1" max="5">
Upvotes: 2
Reputation: 410
isNumber is not receives event object, it is receiving textbox as a parameter.
you could try this: document.getElementById("placesvis").addEventListener('keypress', isNumber);
Upvotes: 0