Is number key pressed in input text field box - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Input Number

Description

Is number key pressed in input text field box

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) {/*w  w w  .  j  a  v  a  2  s .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