Validate if html input field is a number within range - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Input Number

Description

Validate if html input field is a number within range

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=function(){/*  w w  w  . j a  v a2s . c  o  m*/
document.getElementById('test').onchange = isNumber;
function isNumber(){
    var val = this.value;
    var aNumber = !isNaN(val);
    var aNumberNotOverOneHundred = (Math.abs(parseFloat(val, 10)) <= 100);
    console.log((aNumber && aNumberNotOverOneHundred)?"Yarp":"Narp");
}
    }

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

Related Tutorials