js限制文本框输入数字

<!--input只允许输入整数-->
<input type="text" name="a" onkeyup="value=value.replace(/[^\d]/g,'')" >
 
<!--input只允许输入整数和小数(小数只保留小数点后两位)-->
<input type="text" name="aa" onkeyup="value=value.match(/\d+\.?\d{0,2}/,'')" >
<input type="text" name="aa" oninput="value=value.match(/\d*(\.\d{0,2})?/)[0]" >
 
<!--限制文本框只能输入正数、小数、负数-->
<input type="text" onkeyup="this.value=this.value.replace(/[^\-?\d.]/g,'')"/>
        function keyup(index,e) {
            if (index < 5 || index == 10) {
                e.value = e.value.match(/\d+\.?\d{0,3}/, '')
            } else if (index < 7 || index == 11 || index == 13) {
                e.value = e.value.replace(/[^\d]/g, '');
            } else if (index == 12 || index == 14) {
                e.value = e.value.match(/\d+\.?\d{0,1}/, '')
            }
        }
        function changeVal(index, e) {
            var value = e.value;
            if (value == '') {
                return;
            }

            if (index < 5 || index == 10) {
                e.value = Number(value).toFixed(3);
            } else if (index < 7 || index == 11 || index == 13) {
                e.value = Number(value).toFixed(0);
            } else if (index == 12 || index == 14) {
                e.value = Number(value).toFixed(1);
            }
        }
posted @ 2024-07-02 10:13  暖暖De幸福  阅读(125)  评论(0编辑  收藏  举报