js 获取身份证号码出生日期,籍贯等信息

时间:2024-02-19 17:13:14
  1     <script type="text/javascript">
  2         $(document).ready(function () {
  3             $("#<%=tbxIDNumber.ClientID %>").blur(function () {
  4                 var idNumber = $("#<%=tbxIDNumber.ClientID %>").val();
  5                 if (trim(idNumber).length > 0) {
  6                     if (!checkIdcard(idNumber)) {
  7                         alert("输入的身份证号格式错误");
  8                         return;
  9                     }
 10                     else {
 11                         var birthday = getBirthdatByIdNo(idNumber);
 12                         $("#<%=tbxBirthday.ClientID %>").val(birthday);
 13                         var provinceName = getProvinceNameByIdNo(idNumber);
 14                         $("#<%=tbxNativePlace.ClientID %>").val(provinceName);
 15                     }
 16                 }
 17             })
 18         }
 19         );
 20 
 21         function trim(s) { return s.replace(/^\s+|\s+$/g, ""); };
 22 
 23         //验证身份证号并获取籍贯
 24         function getProvinceNameByIdNo(idcard) {
 25             var area = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古",
 26                 21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏",
 27                 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北",
 28                 43: "湖南", 44: "广东", 45: "广西",
 29                 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "*", 61: "陕西",
 30                 62: "甘肃", 63: "青海", 64: "宁夏",
 31                 65: "*", 71: "*", 81: "香港", 82: "澳门", 91: "国外"
 32             }
 33 
 34             var provinceName = "";
 35             var provinceNo = idcard.substr(0, 2);
 36             if (area[parseInt(provinceNo)] != null) {
 37                 provinceName = area[parseInt(provinceNo)];
 38             }
 39             return provinceName;
 40         }
 41 
 42         //获取出生日期
 43         function getBirthdatByIdNo(iIdNo) {
 44             var tmpStr = "";
 45             var strReturn = "";
 46 
 47             if (iIdNo.length == 15) {
 48                 tmpStr = iIdNo.substring(6, 12);
 49                 tmpStr = "19" + tmpStr;
 50                 tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6)
 51                 return tmpStr;
 52             }
 53             else {
 54                 tmpStr = iIdNo.substring(6, 14);
 55                 tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6)
 56                 return tmpStr;
 57             }
 58         }
 59         function checkIdcard(num) {
 60             num = num.toUpperCase();
 61             //身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X。   
 62             if (!(/(^\d{15}$)|(^\d{17}([0-9]|X)$)/.test(num))) {
 63                 //alert(\'输入的身份证号长度不对,或者号码不符合规定!\n15位号码应全为数字,18位号码末位可以为数字或X。\'); 
 64                 return false;
 65             }
 66             //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。 
 67             //下面分别分析出生日期和校验位 
 68             var len, re;
 69             len = num.length;
 70             if (len == 15) {
 71                 re = new RegExp(/^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/);
 72                 var arrSplit = num.match(re);
 73 
 74                 //检查生日日期是否正确 
 75                 var dtmBirth = new Date(\'19\' + arrSplit[2] + \'/\' + arrSplit[3] + \'/\' + arrSplit[4]);
 76                 var bGoodDay;
 77                 bGoodDay = (dtmBirth.getYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));
 78                 if (!bGoodDay) {
 79                     //alert(\'输入的身份证号里出生日期不对!\');   
 80                     return false;
 81                 }
 82                 else {
 83                     //将15位身份证转成18位 
 84                     //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。 
 85                     var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
 86                     var arrCh = new Array(\'1\', \'0\', \'X\', \'9\', \'8\', \'7\', \'6\', \'5\', \'4\', \'3\', \'2\');
 87                     var nTemp = 0, i;
 88                     num = num.substr(0, 6) + \'19\' + num.substr(6, num.length - 6);
 89                     for (i = 0; i < 17; i++) {
 90                         nTemp += num.substr(i, 1) * arrInt[i];
 91                     }
 92                     num += arrCh[nTemp % 11];
 93                     return true;
 94                 }
 95             }
 96             if (len == 18) {
 97                 re = new RegExp(/^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/);
 98                 arrSplit = num.match(re);
 99 
100                 //检查生日日期是否正确 
101                 dtmBirth = new Date(arrSplit[2] + "/" + arrSplit[3] + "/" + arrSplit[4]);
102                 // var bGoodDay;
103                 bGoodDay = (dtmBirth.getFullYear() == Number(arrSplit[2])) && ((dtmBirth.getMonth() + 1) == Number(arrSplit[3])) && (dtmBirth.getDate() == Number(arrSplit[4]));
104                 if (!bGoodDay) {
105                     //alert(dtmBirth.getYear()); 
106                     //alert(arrSplit[2]); 
107                     //alert(\'输入的身份证号里出生日期不对!\'); 
108                     return false;
109                 }
110                 else {
111                     //检验18位身份证的校验码是否正确。 
112                     //校验位按照ISO 7064:1983.MOD 11-2的规定生成,X可以认为是数字10。 
113                     var valnum;
114                     arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
115                     arrCh = new Array(\'1\', \'0\', \'X\', \'9\', \'8\', \'7\', \'6\', \'5\', \'4\', \'3\', \'2\');
116                     nTemp = 0, i;
117                     for (i = 0; i < 17; i++) {
118                         nTemp += num.substr(i, 1) * arrInt[i];
119                     }
120                     valnum = arrCh[nTemp % 11];
121                     if (valnum != num.substr(17, 1)) {
122                         //alert(\'18位身份证的校验码不正确!应该为:\' + valnum); 
123                         return false;
124                     }
125                     return true;
126                 }
127             }
128             return false;
129         }  
130     </script>