Restrict characters in input field - Javascript jQuery

Javascript examples for jQuery:Form Input

Description

Restrict characters in input field

Demo Code

ResultView the demo in separate window

<html>
 <head> 
  <meta name="viewport" content="width=device-width, initial-scale=1"> 
  <script type="text/javascript" src="https://code.jquery.com/jquery-1.10.1.js"></script> 
  <script type="text/javascript">
    $(window).load(function(){/*from   ww  w  .  j  av a2  s .  c  om*/
$('.input').keyup(function () {
    if (!this.value.match(/[0-9]/)) {
        this.value = this.value.replace(/[^0-9]/g, '');
    }
});
    });

      </script> 
 </head> 
 <body> 
  <input class="input" maxlength="1" type="text" autocomplete="off">  
 </body>
</html>

Related Tutorials