Join WhatsApp ChannelJoin Now

How to allow only numbers in InputText In Jquery

Hi Dev,

Today, i we will show you how to allow only numbers in InputText in jquery. This article will give you simple example of how to allow only numbers in InputText in jquery. you will how to allow only numbers in InputText in jquery. So let’s follow few step to create example of how to allow only numbers in InputText in jquery.

Example:

<!DOCTYPE html>
<html>
<head>
  <title>How to allow only numbers in InputText In Jquery</title>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.0/jquery.min.js"></script>
</head>
<body>
   
<div class="container">
  <h1>How to allow only numbers in InputText In Jquery</h1>
         
      <label>Enter Value:</label>
      <input type="text" name="myValue" class="allow-numeric" >
      <span class="error" style="color: red; display: none">* Input digits (0 - 9)</span>
    
</div>
    
<script type="text/javascript">
    
    $(document).ready(function() {
      $(".allow-numeric").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>

I hope it will assist you…

Recommended Posts