asp.net input文字框只能輸入數字js

暖楓無敵發表於2012-03-12

js:

<script type="text/javascript" language="javascript">
        //只能輸入數字
        function ValidateNumber(e, pnumber) {
            if (!/^\d+$/.test(pnumber)) {
                var newValue = /^\d+/.exec(e.value);
                if (newValue != null) {
                    e.value = newValue;
                }
                else {
                    e.value = "";
                }
            }
            return false;
        }

        //可以輸入帶小數點的數字
        function ValidateFloat(e, pnumber) {
            if (!/^\d+[.]?\d*$/.test(pnumber)) {
                var newValue = /^\d+[.]?\d*/.exec(e.value);
                if (newValue != null) {
                    e.value = newValue;
                }
                else {
                    e.value = "";
                }
            }
            return false;
        }

        //只可以輸入一位小數點數字
        function ValidateFloat2(e, pnumber) {
            if (!/^\d+[.]?[1-9]?$/.test(pnumber)) {
                var newValue = /\d+[.]?[1-9]?/.exec(e.value);
                if (newValue != null) {
                    e.value = newValue;
                }
                else {
                    e.value = "";
                }
            }
            return false;
        }
    </script>
<input runat="server" id="t1" type="text" style="width: 30px"
        onkeydown="if(event.keyCode==13)event.keyCode=9"
        onkeypress="if ((event.keyCode<48 || event.keyCode>57)) event.returnValue=false"
        onkeyup="return ValidateFloat(this,value)" />



即可


相關文章