自动计算年龄问题
有三个文本框,一个添生日,一个添当前日期,如何在第三个文本框中显示年龄
------解决方案-------------------- <script language= "JavaScript " type= "text/javascript ">
<!--
function IsValidDate(str)
{
var reg = /^(\d{4})-(\d{1,2})-\d{1,2}$/;
if(!str.match(reg))
{
return false;
}
}
function chgDate()
{
var sForm = document.form1;
var bDate = sForm.birthDay.value;
var cDate = sForm.currDay.value;
if (!IsValidDate(bDate))
{
alert( "日期格式不正确,格式:yyyy-mm-dd ");
return;
}
if(bDate== " "||cDate== " ")
{
alert( "生日或当前日期不能为空 ");
return;
}
var bArray = bDate.split( "- ");
var cArray = cDate.split( "- ");
var k = eval(cArray[0]-bArray[0]);
if(k> =0)
{
sForm.age.value=k;
}
else
{
alert( "当前日期不能小于生日 ");
}
}
//-->
</script>
</head>
<body>
<form name= "form1 ">
生日:
<input type= "text " name= "birthDay " onchange= "chgDate() "> yyyy-mm-dd格式
<br>
当前:
<input type= "text " name= "currDay " value= " <%=date%> ">
<br>
年龄:
<input type= "text " name= "age ">
</form>