jQuery code to allow only numbers in textbox

Below jQuery code snippets will allow only numbers in the textbox. However backspace and delete keys are also allowed.
$(document).ready(function(){ 
   $("#<%= txtNumbers.ClientID%>").keydown(function(e) 
   {
       if (e.shiftKey) 
           e.preventDefault();
       else 
       {
           var nKeyCode = e.keyCode;
           //Ignore Backspace and Tab keys
           if (nKeyCode == 8 || nKeyCode == 9)
               return;
           if (nKeyCode < 95) 
           {
               if (nKeyCode < 48 || nKeyCode > 57)
                   e.preventDefault();
           }
           else 
           {
               if (nKeyCode < 96 || nKeyCode > 105) 
               e.preventDefault();
           }
       }
   });
});
See live Demo and Code
Also read my related posts, which describe how to allow arrow keys with number, alphabets and specific characters.
Feel free to contact me for any help related to jQuery. I will gladly help you.
SHARE

Nothing shocks me, I'm a Software Engineer. And I am not young enough to know everything. I live in World Wide Web and from there take care of this website. This website communicates about my work, learning and experience. I believe life is short, and it is for loving, sharing, learning and connecting. So lets connect..