on Thursday, January 11, 2007
Updated Friday, April 22, 2016
This solution works in both IE and Netscape/Mozilla.
Try it here! Just type in the text box below. Note that digits are allowed and alphabetic characters are not allowed. Observe, too, that arrow keys and backspace are allowed so that you can still edit what you type.
Example: Allow only numbers/digits in TextBox
var charCode = (evt.which) ? evt.which : event.keyCode
if (charCode > 31 && (charCode < 48 || charCode > 57))
<INPUT id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar">