有需要的朋友,可以参考下。
完整代码。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>从身份证中提取出生日期、年龄、性别_www.jb200.com</title>
<script language="javascript">
function xianshi()
{
var nian,yue,ri,action,csrq,age,sex,xingbie,s,y,d,g;
var s=document.form1.sfz.value;
var d=new Date();
//身份证为15位
if (document.form1.sfz.value.length==15 )
{
nian=s.substr(6,2);
yue=s.substr(8,2);
ri=s.substr(10,2);
document.form1.csrq.value="/nian+"-"+yue+"-"+ri;
xingbie=s.substr(14,1);
if" (xingbie%2==0)
document.form1.sex.value="/女";
else
document.form1.sex.value="男";
age=d.getYear()-1900-nian;
document.form1.age.value=age;
}
//身份证为18位
if (document.form1.sfz.value.length==18 )
{
nian=s.substr(6,4);
yue=s.substr(10,2);
ri=s.substr(12,2);
csrq=nian+"-"+yue+"-"+ri;
document.form1.csrq.value="/csrq;
xingbie=s.substr(17,1);
if" (xingbie%2==0)
document.form1.sex.value="/女";
else
document.form1.sex.value="男";
age=d.getYear()-nian;
document.form1.age.value=age;
}
}
</script>
</head>
<body>
<table width="80%" border="1" align="center" style="border:#444444 1px solid;">
<tr>
<td align="center"><font color="#9966FF"><strong生日期、年龄、性别</strong></font></td>
</tr>
</table><br>
<form name="form1" method="post" action="">
<table width="50%" border="1" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="24%" align="center">身份证号:</td>
<td width="76%"><input type="text" name="sfz" onBlur="xianshi()"></td>
</tr>
<tr>
<td align="center">出生日期:</td>
<td><input type="text" name="csrq"></td>
</tr>
<tr>
<td align="center">年 龄:</td>
<td><input type="text" name="age"></td>
</tr>
<tr>
<td align="center">性 别:</td>
<td><input type="text" name="sex"></td>
</tr>
</table>
</form>
</body>
</html>