only number validation in jquery

only number validation in jquery Query : – How to allow only numeric (0-9) in HTML inputbox using jQuery?.

only number validation in jquery

jQuery: Code to allow only numeric values. Textbox accept only numbers (digits) using jquery. If you need to add jquery validation for your textbox like textbox should accept only numbers values on keypress event.

How to Only Allow Numbers in a Text Box using jQuery?

index.html

<!DOCTYPE html>
<html>
<head>
  <title>JQuery - Allow only numeric values in Textbox - www.pakainfo.com</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body>
   
<div class="container">
  <h1>JQuery - Allow only numeric values in Textbox - www.pakainfo.com</h1>
         
      <label>Enter Mobile Number:</label>
      <input type="text" name="tamilName" class="check-mobile-no" >
      <span class="error" style="color: red; display: none">* Input digits (0 - 9)</span>
    
</div>
    
<script type="text/javascript">
    
    $(document).ready(function() {
      $(".check-mobile-no").bind("keypress", function (e) {
          var keyCode = e.which ? e.which : e.keyCode
               
          if (!(keyCode >= 48 && keyCode <= 57)) {
            $(".error").css("display", "inline");
            return false;
          }else{
            $(".error").css("display", "none");
          }
      });
    });
     
</script>
      
</body>
</html>

Example 2

<!DOCTYPE html>
<html>
<head>
  <title>JQuery - Allow only numeric values Textbox - www.pakainfo.com</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body>
   
<div class="container">
  <h1>JQuery - Allow only numeric values in Textbox - www.pakainfo.com</h1>
         
      <label>Enter Value:</label>
      <input type="text" name="tamilName" class="check-mobile-no" >
      <span class="error" style="color: red; display: none">* Input digits (0 - 9)</span>
    
</div>
    
<script type="text/javascript">
    
    $(document).ready(function() {
      $(".check-mobile-no").on("keypress keyup blur",function (event) {    
           $(this).val($(this).val().replace(/[^\d].+/, ""));
            if ((event.which < 48 || event.which > 57)) {
                $(".error").css("display", "inline");
                event.preventDefault();
            }else{
            	$(".error").css("display", "none");
            }
        });
    });
     
</script>
      
</body>
</html>

Don’t Miss : 10 digit mobile number validation in javascript

I hope you get an idea about only number validation in jquery.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.