javascript 验证身份证

  1 /*身份证号码检索*/
  2 function cardCheck(cartNo) {
  3     if (cartNo.val() === "") {
  4         return false;
  5     }
  6     else if (cartNo.val().substring(0, 1) === " " || cartNo.val().substring(cartNo.val().length - 1, cartNo.val().length) === " ") {
  7         return false;
  8     }
  9     else if (/\w+\s+\w+/g.test(cartNo.val())) {
 10         return false;
 11     }
 12     else if (isCardNo(cartNo.val()) === false) {
 13         return false;
 14     }
 15     else if (checkProvince(cartNo.val()) === false) {
 16         return false;
 17     }
 18     else if (checkBirthday(cartNo.val()) === false) {
 19         return false;
 20     }
 21     else if (checkParity(cartNo.val()) === false) {
 22         return false;
 23     }
 24     return true;
 25 }
 26 
 27 /*身份证号码区号*/
 28 var vcity = {
 29     11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古",
 30     21: "辽宁", 22: "吉林", 23: "黑龙江", 31: "上海", 32: "江苏",
 31     33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南",
 32     42: "湖北", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆",
 33     51: "四川", 52: "贵州", 53: "云南", 54: "西藏", 61: "陕西", 62: "甘肃",
 34     63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门", 91: "国外"
 35 }
 36 
 37 /*检查号码是否符合规范,包括长度,类型*/
 38 function isCardNo(cartNo) {
 39     //身份证号码为15位或者18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
 40     var reg = /(^\d{15}$)|(^\d{17}(\d|X)$)|(^\d{15}$)|(^\d{17}(\d|x)$)/;
 41     if (reg.test(cartNo) === false) {
 42         return false;
 43     }
 44     return true;
 45 }
 46 
 47 /*取身份证前两位,校验省份*/
 48 function checkProvince(cartNo) {
 49     var province = cartNo.substr(0, 2);
 50     if (vcity[province] == undefined) {
 51         return false;
 52     }
 53     return true;
 54 }
 55 
 56 //检查生日是否正确*/
 57 function checkBirthday(cartNo) {
 58     var len = cartNo.length;
 59     /*身份证15位时,次序为省(3位)市(3位)年(2位)月(2位)日(2位)校验位(3位),皆为数字*/
 60     if (len === 15) {
 61         var re_fifteen = /^(\d{6})(\d{2})(\d{2})(\d{2})(\d{3})$/;
 62         var arr_data = cartNo.match(re_fifteen);
 63         var year = arr_data[2];
 64         var month = arr_data[3];
 65         var day = arr_data[4];
 66         var birthday = new Date(‘19‘ + year + ‘/‘ + month + ‘/‘ + day);
 67         return verifyBirthday(‘19‘ + year, month, day, birthday);
 68     }
 69     //身份证18位时,次序为省(3位)市(3位)年(4位)月(2位)日(2位)校验位(4位),校验位末尾可能为X*/
 70     if (len === 18) {
 71         var re_eighteen = null;
 72         if (cartNo.substring(17, 18) == "x") {
 73             re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|x)$/;
 74         }
 75         if (cartNo.substring(17, 18) == "X") {
 76             re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9]|X)$/;
 77         }
 78         if (cartNo.substring(17, 18) != "X" && cartNo.substring(17, 18) != "x") {
 79             re_eighteen = /^(\d{6})(\d{4})(\d{2})(\d{2})(\d{3})([0-9])$/;
 80         }
 81         var arr_data = cartNo.match(re_eighteen);
 82         var year = arr_data[2];
 83         var month = arr_data[3];
 84         var day = arr_data[4];
 85         var birthday = new Date(year + ‘/‘ + month + ‘/‘ + day);
 86         return verifyBirthday(year, month, day, birthday);
 87     }
 88     return false;
 89 }
 90 
 91 /*校验日期*/
 92 verifyBirthday = function (year, month, day, birthday) {
 93     var now = new Date();
 94     var now_year = now.getFullYear();
 95     //年月日是否合理*/
 96     if (birthday.getFullYear() == year && (birthday.getMonth() + 1) == month && birthday.getDate() == day) {
 97         //判断年份的范围(3岁到100岁之间)*/
 98         var time = now_year - year;
 99         if (time >= 3 && time <= 100) {
100             return true;
101         }
102         return false;
103     }
104     return false;
105 }
106 
107 /*校验位的检测*/
108 function checkParity(cartNo) {
109     //15位转18位*/
110     cartNo = changeFivteenToEighteen(cartNo);
111     if (cartNo.length === 18) {
112         if (cartNo.substring(17, 18) === "x") {
113             cartNo = cartNo.substring(0, 17) + "X";
114         }
115         var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
116         var arrCh = new Array(‘1‘, ‘0‘, ‘X‘, ‘9‘, ‘8‘, ‘7‘, ‘6‘, ‘5‘, ‘4‘, ‘3‘, ‘2‘);
117         var cardTemp = 0, i, valnum;
118         for (i = 0; i < 17; i++) {
119             cardTemp += cartNo.substr(i, 1) * arrInt[i];
120         }
121         valnum = arrCh[cardTemp % 11];
122         if (valnum === cartNo.substr(17, 1)) {
123             return true;
124         }
125         return false;
126     }
127     return false;
128 }
129 
130 /*15位转18位身份证号*/
131 function changeFivteenToEighteen(cartNo) {
132     if (cartNo.length === 15) {
133         var arrInt = new Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
134         var arrCh = new Array(‘1‘, ‘0‘, ‘X‘, ‘x‘, ‘9‘, ‘8‘, ‘7‘, ‘6‘, ‘5‘, ‘4‘, ‘3‘, ‘2‘);
135         var cardTemp = 0, i;
136         cartNo = cartNo.substr(0, 6) + ‘19‘ + cartNo.substr(6, cartNo.length - 6);
137         for (i = 0; i < 17; i++) {
138             cardTemp += cartNo.substr(i, 1) * arrInt[i];
139         }
140         cartNo += arrCh[cardTemp % 11];
141         return cartNo;
142     }
143     return cartNo;
144 }
View Code

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。