原试如下:
<input onpaste="return false;" onkeyup="this.value=this.value.replace(/\D/g,'');">
目的是文本框只能输入数字,但我的文本框要只能输入数字或浮点的
也就是说可以输入 N位数字、一个“.”、最多小数点后两位数字;
当然也可以只是数字,不必非得是浮点。
谢谢!
6 个解决方案
#1
onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo'可以满足要求
#2
小数点后只能有最多两位(数字,中文都可输入),不能输入字母和运算符号:
CODE:
<input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false">
<input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false">
小数点后只能有最多两位(数字,字母,中文都可输入),可以输入运算符号
CODE:
<input onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')">
<input onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')">
CODE:
<input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false">
<input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false">
小数点后只能有最多两位(数字,字母,中文都可输入),可以输入运算符号
CODE:
<input onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')">
<input onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')">
#3
<input onpaste="return false;" onkeyup="this.value=this.value=(function (a) {return a.length > 1 ? a.shift().replace(/\D/g, '') + '.' + a.join('').replace(/\D/g, '').slice(0, 2) : a[0].replace(/\D/g,'');})(this.value.split('.'))" onBlur="this.value = this.value.replace(/^\D|\D$/, '')">
#4
alert(/^[1-9]\d*(\.\d{1,2})?$|^0\.\d{1,2}$|^0$/.test("0.111"));
#5
<input onpaste= "return false; " onkeyup= "this.value=this.value.replace(/\\D/g, ' '); ">
#6
我也遇到这个问题,还是不清楚replace(/'/g,"‘");是什么含义?
#1
onkeyup="if(isNaN(value))execCommand('undo')" onafterpaste="if(isNaN(value))execCommand('undo'可以满足要求
#2
小数点后只能有最多两位(数字,中文都可输入),不能输入字母和运算符号:
CODE:
<input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false">
<input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false">
小数点后只能有最多两位(数字,字母,中文都可输入),可以输入运算符号
CODE:
<input onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')">
<input onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')">
CODE:
<input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false">
<input onKeyPress="if((event.keyCode<48 || event.keyCode>57) && event.keyCode!=46 || /\.\d\d$/.test(value))event.returnValue=false">
小数点后只能有最多两位(数字,字母,中文都可输入),可以输入运算符号
CODE:
<input onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')">
<input onkeyup="this.value=this.value.replace(/^(\-)*(\d+)\.(\d\d).*$/,'$1$2.$3')">
#3
<input onpaste="return false;" onkeyup="this.value=this.value=(function (a) {return a.length > 1 ? a.shift().replace(/\D/g, '') + '.' + a.join('').replace(/\D/g, '').slice(0, 2) : a[0].replace(/\D/g,'');})(this.value.split('.'))" onBlur="this.value = this.value.replace(/^\D|\D$/, '')">
#4
alert(/^[1-9]\d*(\.\d{1,2})?$|^0\.\d{1,2}$|^0$/.test("0.111"));
#5
<input onpaste= "return false; " onkeyup= "this.value=this.value.replace(/\\D/g, ' '); ">
#6
我也遇到这个问题,还是不清楚replace(/'/g,"‘");是什么含义?