Force specific pattern in input field using regex and javascript - Javascript jQuery

Javascript examples for jQuery:Form Input

Description

Force specific pattern in input field using regex and javascript

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.9.1.js"></script> 
  <script type="text/javascript">
    $(function(){//from  w  w  w . j ava 2 s.c o m
$('input').on('change keyup keydown',function(){
    var txt = $(this).val();
    $(this).val(/^P\d*$/.test(txt)&txt.length<10?txt:txt.substring(0,txt.length-1));
});
    });

      </script> 
 </head> 
 <body> 
  <input type="text">  
 </body>
</html>

Related Tutorials