Pages

10 Sept 2013

jQuery code to allow only numbers in textbox

 jQuery, jQuery Code Examples, Jquery Code Snippets, jQuery Codes

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();
           }
       }
   });
});

No comments:

Post a Comment