Check key code in key event handler - Javascript DOM Event

Javascript examples for DOM Event:which

Description

Check key code in key event handler

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
   </head> 
   <body> 
      <input id="txtChar" onkeypress="return isNumberKey(event)" type="text" name="txtChar"> 
      <script type="text/javascript">
function isNumberKey(evt) {//from w  w  w  .jav  a2s .c om
    var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;
    else
        return true;
}

      </script>  
   </body>
</html>

Related Tutorials