<!--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);
}
}
标签:index,Number,value,else,文本框,toFixed,js,输入,match
From: https://www.cnblogs.com/duixue/p/18279372