<form name=form1><input type=text onpropertychange="if (this.value==this.value2) return; if (this.value.search(/^\d*(?:\.\d{0,2})?$/)==-1) this.value=(this.value2)?this.value2:'';else this.value2=this.value;"></form>应该可以了,你把onpropertychange=........... 贴上去用就可以,解释起来太麻烦……
我看了,不可以呀。
还有能在输入的时候就执行吗,不要切换光标的时候这样。
var a,b
a=document.form1.text1.value
b=a.lenght
if (b==5){
if (a.substring(0,1)%2==1){
if(a.substring(0,1)%2==0){
if(a.substring(1,2)%2==1{
if(a.substring(1,2)%2==0){
if(a.substing(2,3)=="."){
if(a.substring(3,4)%2==0){
if(a.substring(3,4)%2==1){
if(a.substring(4,5)%2==0){
if(a.substring(4,5)==0){
}
else{
document.alert(输入有误)
document.form1.text1.focus()
}
else{
if(a.substring(4,5)==""){
}
else{
document.alert(输入有误)
document.form1.text1.focus()
}
}
}
else{
document.alert(输入有误)
document.form1.text1.focus()
}
}
}
}
}
}
}
<script>
function aa(){
var a,b,c,d
a=document.form1.text1.value
var b=a.lenght
if (b<=5){
for (c=0;c=b;c++){
d=1+c
if(a.substr(c,d)%2==0){
if(a.substr(c,d)%2==1){
}
else{
document.alert("输入有误")
document.form1.text1.focus()
document.form1.value=""
}
}
if (c==2){
if (a.substr(c,d)=="."){
}
else{
document.alert("输入有误")
document.form1.text1.focus()
document.form1.value=""
}
}
if (c==3){
if(a.substr(c,d)==""){
}
else{
if (a.substr(c,d)%2==1){
if(a.substr(c,d)%2==0){
}
else{
document.alert("输入有误")
document.form1.text1.focus()
document.form1.value=""
}
if (c==4){
if(a.substr(c,d)==""){
}
else{
if (a.substr(c,d)%2==1){
if(a.substr(c,d)%2==0){
}
else{
document.alert("输入有误")
document.form1.text1.focus()
document.form1.value=""
}
}
}
}
else{
document.alert("输入有误")
document.form1.text1.focus()
document.form1.value=""
}
}
}
abc=setTimeout("aa();10)
aa();
</script>
<form name=form1 action="">
<input type="text" name="text1">
或
<input type="text" name="text1" onclick=aa()>
</form>
<script language="JavaScript">
function check()
{
var k = event.keyCode
var v = t.value
var hasdot = (v.indexOf(".")==-1)?false:true
var l = v.length
if (event.shiftKey) event.returnValue = false
else if (l == 0)
{
if (k<48 && k!=8 && k!=36||k>57 && k<96 || k>105 && k!=109 && k!=189) event.returnValue = false
}
else if (l == 1)
{
if (v == "-")
{
if (k<48 && k!=8 && k!=36||k>57 && k<96 || k>105) event.returnValue = false
}
else if (v == "0")
{
if (k!= 8 && k!= 36 && k!=110 && k!=190) event.returnValue = false
}
else
{
if (k<48 && k!=8 && k!=36||k>57 && k<96 || k>105 && k!=110 && k!=190) event.returnValue = false
}
}
else if (v=="-0")
{
if (k<=48 && k!=8 && k!=36||k>57 && k<=96 || k>105 && k!=110 && k!=190) event.returnValue = false
}
else
{
if (k<48 && k!=8 && k!=36||k>57 && k<96 || k>105 && k!=110 && k!=190) event.returnValue = false
if (hasdot && (k==110 || k==190)) event.returnValue = false
}
}
</script>
<input type="text" id="t" onKeyDown="check()">
if (hasdot && v.indexOf(".")+3==l)
{
event.returnValue = false
return
}限制粘贴在input 加onpaste="return false"
下面是我的代码:(可以限制输入数字之外的其他字符,但不能控制小数点后两位
)
<input type="text" name="textfield2" onkeypress="return event.keyCode>=48&&event.keyCode<=57||event.keyCode==46"
onpaste="return !clipboardData.getData('text').match(/\D/)"
ondragenter="return false"
style="ime-mode:Disabled">
大家可以自己使用,在上面的代码中改改
要是有问题,你在问我.绝对帮你解决.<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script>
function test(str){
var pos;
var fst
var lst;
if (str == "") return;
pos = str.indexOf(".");
if (pos != -1){
fst = str.substring(0,pos);
lst = str.substring(pos+1,pos.length);
if (lst.length > 2){
var sub = lst.substring(0,2);
textfield2.value=fst+"."+sub;
}
}
}
</script>
</HEAD><BODY>
<input type="text" name="textfield2" onkeyup="test(this.value)"
onpaste="return !clipboardData.getData('text').match(/\D/)"
ondragenter="return false"
style="ime-mode:Disabled">
</BODY>
</HTML>