一般都是通过正则表达式进行判断的。下面是简单的代码实现。仅供参考:
1.文本框只能输入数字代码(小数点也不能输入)
<input onkeyup=
"this.value=this.value.replace(/\D/g,'')"
onafterpaste=
"this.value=this.value.replace(/\D/g,'')"
>
2.只能输入数字,能输小数点.
<input onkeyup=
"if(isNaN(value))execCommand('undo')"
onafterpaste=
"if(isNaN(value))execCommand('undo')"
>
<input name=txt1 onchange=
"if(/\D/.test(this.value)){alert('只能输入数字');this.value='';}"
>
3.数字和小数点方法二
<input type=text t_value=
""
o_value=
""
onkeypress=
"if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/)){
this.value=this.t_value;
} else {
this.t_value=this.value;
}
if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/)) {
this.o_value=this.value"
;
onkeyup=
"if(!this.value.match(/^[\+\-]?\d*?\.?\d*?$/)) {
this.value=this.t_value;
}else {
this.t_value=this.value;
}
if(this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?)?$/)) {
this.o_value=this.value"
;
onblur=
"if(!this.value.match(/^(?:[\+\-]?\d+(?:\.\d+)?|\.\d*?)?$/)){
this.value=this.o_value;
}else if(this.value.match(/^\.\d+$/)) {
this.value=0+this.value;
}
if(this.value.match(/^\.$/)) {
this.value=0;
} this.o_value=this.value}"
>