Validation with jQuery key press event handler - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:keypress

Description

Validation with jQuery key press event handler

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.6.4.js"></script> 
  <script type="text/javascript">
    $(window).load(function(){/* ww w .  java 2  s. c o m*/
$("#txtBox").keypress(function (e) {
    var input = $(this).val() + String.fromCharCode(e.which);
    if (input.split(',').length > 4) {
        e.preventDefault();
    }
});
    });

      </script> 
 </head> 
 <body> 
  <input id="txtBox">  
 </body>
</html>

Related Tutorials