注意生日必须是yyyy-mm-dd 格式,第一次在生日中输入生日,焦点离开后其它显示正常,但是再改生日中的日期后离开焦点,就会报错?????
代码如下:
<html>
<head>
<script language="javascript">
<!--
//登录form校验
function age()
{
var b=form1.b.value;
/*var yearone=parseInt( s.substr(0,s.indexOf("年")) );
var monthone=parseInt( s.substr(s.indexOf("年")+1,s.indexOf("月")) );
var dayone=parseInt( s.substr(s.indexOf("月")+1,s.indexOf("日")) );*/
y=parseInt( b.substr(0,4 ));
m=parseInt( b.substr(5.2 ));
d=parseInt( b.substr(8,2 ));
document.forms['form1'].y.value = y;
document.forms['form1'].m.value = m;
document.forms['form1'].d.value = d;
days = new Date();
nd = days.getDate();
nm = days.getMonth()+1;
ny = days.getYear();
document.forms['form1'].ny.value = ny;
document.forms['form1'].nm.value = nm;
document.forms['form1'].nd.value = nd;
age=0;
if (ny>y){
if(nm>m){ age=ny-y; }
else if(nm<m){ age=ny-y-1; }
else if(nm=m){
if (nd>d){ age=ny-y; }
else if (nd<d){ age=ny-y-1; }
else if (nd=d){ age=ny-y; }
}
}
document.forms['form1'].a.value = age;
}
//-->
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<p>
生日<input name="b" type="text" id="1" onblur="age();" />
年龄<input name="a" type="text" id="a" />
</p>
<p>
生日年<input name="y" type="text" id="y" />
生日月<input name="m" type="text" id="m" />
生日日<input name="d" type="text" id="d" />
</p>
<p>
当前年<input name="ny" type="text" id="ny" />
当前月<input name="nm" type="text" id="nm" />
当前日<input name="nd" type="text" id="nd" />
</p>
</form>
</body>
</html>
[此贴子已经被作者于2007-10-30 16:56:54编辑过]