Textbox that Accepts Only Numbers using JQuery

Step 1: Add this function in your View:
  1. $(document).ready(function () {  
  2.    $(".digit").keypress(function (e) {  
  3.       if (e.which != 8 && e.which != 0 && (e.which < 48 || e.which > 57)) {  
  4.          $("#errormsg").html("Digits Only").show().fadeOut("slow");  
  5.          return false;  
  6.       }  
  7.    });  
  8. });  
Step 2: Add a span next to the TextBoxFor to show the error message and the class name we are using should be same.
Here i have given class name as "digit" and using it when the key is pressed:
  1. <li>  
  2.    @Html.LabelFor(x => x.company.ContactNumber)  
  3.    @Html.TextBoxFor(x => x.company.ContactNumber, new { @class = "digit" })  
  4.    <span id="errormsg"></span>  
  5. </li>  
The textbox will accept only numbers, backspace and delete keys.
I had given the respective key codes in the jquery function.