only allow a user enter "-" and 1-9 into the textbox - Javascript jQuery

Javascript examples for jQuery:Form Text Input

Description

only allow a user enter "-" and 1-9 into the textbox

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.5.2.js"></script> 
  <script type="text/javascript">
    $(window).load(function(){/*from  w w  w.  ja  va 2s  .c  o m*/
$('textarea').keypress(function(event) {
    if ( ! String.fromCharCode(event.keyCode).match(/[\d-]/)) {
          event.preventDefault();
    }
});
    });

      </script> 
 </head> 
 <body> 
  <textarea>
</textarea>  
 </body>
</html>

Related Tutorials