Showing posts with label How to allow only numeric (0-9) in inputbox using jQuery. Show all posts
Showing posts with label How to allow only numeric (0-9) in inputbox using jQuery. Show all posts

Friday, 17 October 2014

jQuery textbox to allow only numbers/digits


jQuery textbox to allow only numbers/digits

Here I will show how to use jQuery to allow only numbers in textbox or allow only numeric characters in textbox using JQuery. Using jQuery we can control user input with different key event handlers. So in this post I am going to share the jQuery code to allow only numbers in textbox.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<input type="text" name="mobile" id="mob_number" />

<script type="text/javascript">

$(document).ready(function () {
  //called when key is down
  $("#mob_number").bind("keydown", function (event) {
    if ( event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || (event.keyCode == 65 && event.ctrlKey === true) || (event.keyCode >= 35 && event.keyCode <= 39)){
           
 return;

        } else {
            if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
                return false;
            }
        }
   });
});

</script>
</body>
</html>