Restrict user to enter numeric value in textbox using javascript

Shailendra Chauhan  Print   1 min read  
11 Jun 2012
29 Jan 2013

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.

 <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;
 event.charCode= 0

<asp:TextBox ID="TextBox1" runat="server" onkeypress="return checkNumeric(event); ></asp:TextBox> 

Learn to Crack Your Technical Interview

Learn to Code

Start your journey today to learn coding!

Because learning to code is the first step and foreward to advance your career.