21
FebRestrict user to enter numeric value in textbox using javascript
26 Jan 2025
Intermediate
8.03K Views
1 min read
Due to some reasons (like don’t allow to enter chars in Price TextBox), we restrict users to enter chars in TextBox. We can implement this functionality by using below method. To learn more about handling such cases and other JavaScript techniques, you can enroll in a Free Online JavaScript Course and enhance your skills!
<script type="text/javascript"> function checkNumeric(event) { var kCode = event.keyCode || event.charCode; // for cross browser check //FF and Safari use e.charCode, while IE use e.keyCode that returns the ASCII value if ((kCode > 57 || kCode < 48) && (kCode != 46 && kCode != 45)) { //code for IE if (window.ActiveXObject) { event.keyCode = 0 return false; } else { event.charCode= 0 } } } </script> <asp:TextBox ID="TextBox1" runat="server" onkeypress="return checkNumeric(event); ></asp:TextBox>
Read More: 50+ Javascript Interview Questions and Answers
Read More: Javascript Developer Salary in India
Take our Javascript skill challenge to evaluate yourself!

In less than 5 minutes, with our skill challenge, you can identify your knowledge gaps and strengths in a given skill.