JS中输入身份证号码,subString截取出生日,并判断性别

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title></title>
</head>
<body>
<p><input type="text" id="sfz" placeholder="请输入身份证号" /></p>
<p><input type="button" id="sub" value="确定" onclick="myfun()" /></p>
<p>您的生日是:<span id="bir"></span></p>
<p>您的性别为:<span id="sex"></span></p>
<script type="text/javascript">
function myfun () {
var str=document.getElementById("sfz").value;
var b=document.getElementById("bir");
var y=str.substring(6,10);
var m=parseInt(str.substring(10,12));
var d=parseInt(str.substring(12,14));
var sex=str.substring(16,17);
var s=document.getElementById("sex");
if (sex%2==0) {
s.innerHTML="女";
} else{
s.innerHTML="男";
}
b.innerHTML=(y+"年"+m+"月"+d+"日");

}
</script>
</body>
</html>

相关推荐