限制输入到文本框:只允许数字和小数点 [英] Restricting input to textbox: allowing only numbers and decimal point

查看:91
本文介绍了限制输入到文本框:只允许数字和小数点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何限制输入到文本框,以便它只接受数字和小数点?

div class =snippetdata-lang =jsdata-hide =falsedata-console =truedata-babel =false>

 < 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;返回true; } //  - > < / SCRIPT> < / HEAD> < BODY> < INPUT id =txtCharonkeypress =return isNumberKey(event)type =textname =txtChar> < / BODY>< / HTML>  


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

解决方案

<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!

这篇关于限制输入到文本框:只允许数字和小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆