Allow numbers and minus sign only in input box via input action handler - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Input Number

Description

Allow numbers and minus sign only in input box via input action handler

Demo Code

ResultView the demo in separate window

<html lang="en">
   <head> 
      <title>  kecer</title> 
   </head> 
   <body translate="no"> 
      <input id="num" type="text"> 
      <script>

var last = '';

var input = document.getElementById('num');

input.addEventListener('input', function(e){
    if (isNaN(e.target.value))// ww  w .  ja v a  2 s.  c  om
        e.target.value=last;
    else
        last=e.target.value;
});

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

Related Tutorials