Limit to enter only 2 decimal points number into input text field - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Input Number

Description

Limit to enter only 2 decimal points number into input text field

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(){//from   w  w w. java2 s  .co m

var input_field = document.getElementById('the_id');
input_field.addEventListener('change', function() {
    var v = parseFloat(this.value);
    if (isNaN(v)) {
        this.value = '';
    } else {
        this.value = v.toFixed(2);
    }
});

    }

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

Related Tutorials