/ Published in: JavaScript
The following code allows only numeric characters to be entered.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
<html> <head> <script type="text/javascript"> function checkIt(evt) { evt = (evt) ? evt : window.event var charCode = (evt.which) ? evt.which : evt.keyCode if (charCode > 31 && (charCode < 48 || charCode > 57)) { document.getElementById("errorMessage").innerHTML = "This field accepts numbers only." return false } document.getElementById("errorMessage").innerHTML = "" return true } </script> </head> <body> <input id="numbersOnly" type="text" onKeyPress="return checkIt(event)" /> <span id="errorMessage"></span> </body> </html>