参考:http://www.tuicool.com/articles/J7r2ien
方式一:
$scope.GetAgeAndSexByIDNum = function (IdCardNO)
{
// 身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
if (reg.test(IdCardNO) === false)
{
alert("身份证号输入不合法");
return false;
}
var date = new Date();
var yearfull = date.getFullYear();
if (IdCardNO.length == 18)//18位身份证号处理
{
var birthyear = IdCardNO.substring(6, 10);
var month = IdCardNO.substring(10, 12);
var day = IdCardNO.substring(12, 14);
var age = yearfull - birthyear;
var sexValue = IdCardNO.substring(16, 17);
var sex = (sexName % 2 == 0 ? "女" : "男");
if (sex.toString() == "女")
sexValue = "2";
if (sex.toString() == "男")
sexValue = "1";
else sexValue = "3";
$scope.applyForm.Age = age; document.getElementById("sex").value = sexValue;
$scope.patient.sex = sex;
}
if (IdCardNO.length == 15)//15位身份证号处理
{
var birthDay = "";
birthDay = IdCardNO.substring(6, 12);
birthDay = "19" + birthDay;
birthDay = birthDay.substring(0, 4);// + "-" + birthDay.substring(4, 6) + "-" + birthDay.substring(6)
var age = yearfull - birthDay;//年龄
var sex = parseInt(birthDay.substring(14, 1), 10) % 2 ? "男" : "女";//性别
var sexValue = "";
if (sex.toString() == "女")
sexValue = "2";
if (sex.toString() == "男")
sexValue = "1";
else sexValue = "3";
$scope.applyForm.Age = age;
document.getElementById("sex").value = sexValue;
$scope.patient.sex = sex;
}
};
方式二: <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="jquery.min.js"></script>
</head>
<body>
<input type="tel" id="js_Idcard">
<span id="js_birthday"></span>
<script>
$(function(){
function GetBirthdatByIdNo(iIdNo){
var tmpStr = "";
var birthday = $("#js_birthday"); iIdNo = $.trim(iIdNo); if(iIdNo.length == ){
tmpStr = iIdNo.substring(, );
tmpStr = "" + tmpStr;
tmpStr = tmpStr.substring(, ) + "-" + tmpStr.substring(, ) + "-" + tmpStr.substring();
sexStr = parseInt(iIdNo.substring(, ),) % ? "男" : "女";
birthday.text(sexStr + tmpStr);
}else{
tmpStr = iIdNo.substring(, );
tmpStr = tmpStr.substring(, ) + "-" + tmpStr.substring(, ) + "-" + tmpStr.substring();
sexStr = parseInt(iIdNo.substring(, ),) % ? "男" : "女";
birthday.text(sexStr + tmpStr);
}
} $("#js_Idcard").blur(function(){
GetBirthdatByIdNo($(this).val());
});
});
</script>
</body>
</html> 方式三: <!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="jquery.min.js"></script>
</head> <body>
<input type="tel" id="js_Idcard">
<span id="js_birthday"></span>
<script>
$(function(){
function go(){
var id = $("#js_Idcard").val();
var last = id[id.length - ]; if(last % != ){
$("#js_birthday").text("男");
}else{
$("#js_birthday").text("女");
}
} $("#js_Idcard").blur(function(){
go();
});
});
</script>
</body>
</html>