Function Isidcardno (num) { num = Num.touppercase (); //ID number is 15 digits or 18 digits, 15 digits are all digits, 18 Digits, the first 17 digits, and the last one is a check digit, possibly a number or a character X. if (! /(^/d{15}$) | (^/d{17} ([0-9]| X) $)/.test (num)) { alert (' Input ID number is not the right length, or the number does not meet the requirements! /n15 digit number should be full, 18 digits can be the lowest number or X. '); return false; } The //check digit is generated according to ISO 7064:1983.mod 11-2, and X can be considered the number 10. //below analyze birth date and check digit respectively Var len, re; Len = num.length; if (len = =) { Re = new RegExp (/^ (/d{6}) (/d{2}) (/d{2}) (/d{2})/d{3); var arrsplit = Num.match (re); Check if the birthday date is correct var Dtmbirth = new Date (' ' + arrsplit[2] + '/' + arrsplit[3] + '/' + arrsplit[4] "; var bgoodday; Bgoodday = (dtmbirth.getyear () = = number (arrsplit[2]) && ((Dtmbirth.getmonth () + 1) = = number (arrsplit[3)) ;& (dtmbirth.getdate () = = number (arrsplit[4])); if (!bgoodday) { Alert (' entered the ID number in the wrong birth date! '); return false; } Else { Turn 15 ID cards into 18-bit The check digit is generated according to ISO 7064:1983.mod 11-2, and X can be considered to be the number 10. var arrint = new Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); var arrch = new Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 '); var ntemp = 0, I; num = num.substr (0, 6) + ' n ' + num.substr (6, num.length-6); for (i = 0; i < i + +) { Ntemp + + NUM.SUBSTR (i, 1) * arrint[i]; } num + + arrch[ntemp% 11]; return num; } } if (len = = 18) { Re = new RegExp (/^ (/d{6}) (/d{4}) (/d{2}) (/d{2}) (/d{3}) ([0-9]| X) $/); var arrsplit = Num.match (re); Check if the birthday date is correct var Dtmbirth = new Date (arrsplit[2] + "/" + arrsplit[3] + "/" + arrsplit[4]); var bgoodday; Bgoodday = (dtmbirth.getfullyear () = = number (arrsplit[2]) && ((Dtmbirth.getmonth () + 1) = = number (arrsplit[3)) && (dtmbirth.getdate () = = number (arrsplit[4]); if (!bgoodday) { Alert (Dtmbirth.getyear ()); Alert (arrsplit[2]); Alert (' entered the ID number in the wrong birth date! '); return false; } Else { Verify that the check code for the 18-bit ID card is correct. The check digit is generated according to ISO 7064:1983.mod 11-2, and X can be considered to be the number 10. var valnum; var arrint = new Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); var arrch = new Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 '); var ntemp = 0, I; for (i = 0; i < i + +) { Ntemp + + NUM.SUBSTR (i, 1) * arrint[i]; } Valnum = arrch[ntemp% 11]; if (Valnum!= num.substr (17, 1)) { Alert (' 18-bit ID card is not the correct check code! should be: ' + valnum '; return false; } return num; } } return false; } 3, the strict verification: <script> var acity={11: "Beijing", 12: "Tianjin", 13: "Hebei", 14: "Shanxi", 15: "Inner Mongolia", 21: "Liaoning", 22: "Jilin", 23: "Heilongjiang", 31: "Shanghai", 32: " Jiangsu ", 33:" Zhejiang ", 34:" Anhui ", 35:" Fujian ", 36:" Jiangxi ", 37:" Shandong ", 41:" Henan ", 42:" Hubei ", 43:" Hunan ", 44:" Guangdong ", 45:" Guangxi ", 46:" Hainan ", 50:" Chongqing ", 51:" Sichuan ", 52 : "Guizhou", 53: "Yunnan", 54: "Tibet", 61: "Shaanxi", 62: "Gansu", 63: "Qinghai", 64: "Ningxia", 65: "Xinjiang", 71: "Taiwan", 81: "Hong Kong", 82: "Macao", 91: "Foreign"} function Cidinfo (sId) { var isum=0 var info= "" if (!/^d{17} (d|x) $/i.test (sId)) return false; Sid=sid.replace (/x$/i, "a"); if (Acity[parseint (Sid.substr (0,2))]==null) return "Error: illegal area"; Sbirthday=sid.substr (6,4) + "-" +number (Sid.substr (10,2)) + "-" +number (Sid.substr (12,2)); var d=new Date (Sbirthday.replace (/-/g, "/") if (sbirthday!= (d.getfullyear () + "-" + (D.getmonth () +1) + "-" + d.getdate ()) return "Error: illegal Birthday"; for (var i = 17;i>=0;i-) Isum + = (Math.pow (2,i)%) * parseint (Sid.charat (17-i), 11) if (isum%11!=1) return "ERROR: illegal license Number"; Return Acity[parseint (Sid.substr (0,2))]+ ", +sbirthday+", "+ (SID.SUBSTR (16,1)%2?" Male ":" female ") } document.write (Cidinfo ("380524198002300016"), "<br/>"); document.write (Cidinfo ("340524198002300019"), "<br/>") document.write (Cidinfo ("340524197711111111"), "<br/>") document.write (Cidinfo ("34052419800101001x"), "<br/>"); </script> |