Restricting input to textbox: allowing only numbers and decimal point

TinTin picture TinTin · May 11, 2010 · Viewed 354.2k times · Source

How can I restrict input to a text-box so that it accepts only numbers and the decimal point?

Answer

rebisco picture rebisco · May 13, 2010

<HTML>
  <HEAD>
    <SCRIPT language=Javascript>
       <!--
       function isNumberKey(evt)
       {
          var charCode = (evt.which) ? evt.which : evt.keyCode;
          if (charCode != 46 && charCode > 31 
            && (charCode < 48 || charCode > 57))
             return false;

          return true;
       }
       //-->
    </SCRIPT>
  </HEAD>
  <BODY>
    <INPUT id="txtChar" onkeypress="return isNumberKey(event)" 
           type="text" name="txtChar">
  </BODY>
</HTML>

This really works!