js只允许输入数字和小数点 (转)

转自:http://blog.csdn.net/yenange/article/details/6168443

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        //示例代码:
        //只允许输入数字与.:<input type="text" name="test" id="test" οnkeydοwn="checkKeyForFloat(this.value,event)" style="ime-mode: disabled" />
        //只允许输入数字   :<input type="text" name="test2" id="test2" οnkeydοwn="checkKeyForNum(this.value,event)" style="ime-mode: disabled" />
        //只允许输入数字与小数点  
        function checkKeyForFloat(value, e) {
            var isOK = false;
            var key = window.event ? e.keyCode : e.which;
            if ((key > 95 && key < 106) ||                  //小键盘上的0到9
            (key > 47 && key < 60) ||                   //大键盘上的0到9
            (key == 110 && value.indexOf(".") < 0) ||   //小键盘上的.而且以前没有输入.
            (key == 190 && value.indexOf(".") < 0) ||   //大键盘上的.而且以前没有输入.
            key == 8 || key == 9 || key == 46 || key == 37 || key == 39     //不影响正常编辑键的使用(8:BackSpace;9:Tab;46:Delete;37:Left;39:Right)
        ) {
                isOK = true;
            } else {
                if (window.event) //IE  
                {
                    e.returnValue = false;   //event.returnValue=false 效果相同.  
                }
                else //Firefox  
                {
                    e.preventDefault();
                }
            }
            return isOK;
        }

        //只允许输入数字  
        function checkKeyForInt(value, e) {
            var isOK = false;
            var key = window.event ? e.keyCode : e.which;
            if ((key > 95 && key < 106) ||                  //小键盘上的0到9
            (key > 47 && key < 60) ||                   //大键盘上的0到9
            key == 8 || key == 9 || key == 46 || key == 37 || key == 39     //不影响正常编辑键的使用(8:BackSpace;9:Tab;46:Delete;37:Left;39:Right)
        ) {
                isOK = true;
            } else {
                if (window.event) //IE  
                {
                    e.returnValue = false;   //event.returnValue=false 效果相同.  
                }
                else //Firefox  
                {
                    e.preventDefault();
                }
            }
            return isOK;
        }

        //设置有自定义属性 dtype 的文本框 允许输入的范围
        function setDType() {
            $(":text[dtype]").each(function () {
                var dtype = $(this).attr("dtype");
                var isOK = true;
                switch (dtype) {
                    case "number":
                        $(this).css("ime-mode", "disabled").keydown(function (event) {
                            isOK = checkKeyForFloat($(this).val(), event);
                            if (!isOK) {
                                //$(this).SuperFocus("", 500);
                            }
                            return isOK;
                        });
                        break;
                    default:
                        break;
                }
            });
        }
    </script>
    <script type="text/javascript">
        $(function () {
            setDType();
        });
    </script>
</head>
<body>
    年龄: <input type="text" maxlength="3" οnkeydοwn="checkKeyForInt(this.value,event)" style="ime-mode: disabled"/><br />
    身高:<input type="text" maxlength="5" dtype="number" />
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值