According to the ID number coding rules to verify its validity, the specific implementation of the following, you can refer to the following
The following is based on the ID card number coding rules, use JS to validate the code Idcard-validate.js code as follows: code:/** * ID card 15-bit code rule: dddddd YYMMDD xx p * dddddd: Region code * YYMMDD: Date of birth * XX: Sequence class code, unable to determine * P: Gender, odd male, even number for female * <p/>&NB Sp * ID card 18-bit coding rule: dddddd yyyymmdd xxx y * dddddd: Region code * YYYYMMDD: birth date * XXX: Sequence class Code, cannot be determined, odd number is male, even number is female * y: Parity code, which can be computed by the first 17 digits * <P/> * 18-digit weighting factor (from right to left) Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2,1] * Verification bit Y = [1, 0, 9, 8, 7, 6, 5, 4, 3, 2] * Parity calculation formula: y_p = mod (∑ (AIXWI), one) * I for ID number from right to left 2...18 bit; Y_p for Feet Check code array location * */ var Wi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];//weighted Factor var validecode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2];//ID card Verification bit value. 10 represents x function Idcardvalidate (idcard) {&NB Sp Idcard = Trim (idcard.replace (//g, "")); if (idcard.length =) { return Isvaliditybrithby15idcard (Idcard); } ELSE if (idcard.length = =) { var a_idcard = Idcard.split ("");//Get ID array if (Isvaliditybrithby18idcard (Idcard) & Amp;&istruevalidatecodeby18idcard (A_idcard)) { return true; }else { return false; } } else { return false; } } /** * To determine whether the final verification bit is correct when the ID number is 18 * @param a_idcard ID number array * @return */ function Istruevalidatecodeby18idcard (a_idcard) { var sum = 0;//Declare weighted sum variable if (A_IDCA Rd[17].tolowercase () = = ' x ') { a_idcard[17] = 10;//replaces the last bit X's captcha with 10 for subsequent operations } for (var i = 0; i < 17; i++) { sum + + wi[i] * a_idcard[i];//weighted sum } valcodeposition = sum% 11;//Get Authenticode location if (a_idcard[17 ] = = Validecode[valcodeposition]) { return true; } else { return false; } } /** * by ID card is a male or female * @param idcard 15/18 ID number * @return ' female '-female, ' male '-male */ function Maleorfemalby Idcard (idcard) { Idcard= Trim (idcard.replace (//g, ""))//To handle the ID number. Includes spaces between characters. if (idcard.length==15) { if (idcard.substring (14,15)%2==0) { return ' female '; }else{ Return ' male '; } }else if (idcard.length ==18) { if (idcard.substring (14,17)%2==0) { return ' Female '; }else{ return ' male '; } }else{ return null; } //can be processed directly as an array of incoming characters /if (idcard.length==15) { //alert (IDCARD[13)); //if (idcard[13]%2==0) { //return ' female '; nbsp }else{ //return ' male '; //} //}else if (idcard.length==18) { /alert (idcard[16)); //IF (idcard[16]%2==0) { //Return ' female '; //}else{ //return ' male '; //} //}else{ //Return null; } } /** * Verify that the birthdays in the 18-digit ID number are valid birthdays * @param idcard 18-digit ID card string * @return */ Fun Ction Isvaliditybrithby18idcard (IDCARD18) { var year = idcard18.substring (6,10); var month = idcard18.substring (10,12); var day = idcard18.substring (12,14); var temp_date = new Date (year,parsefloat (month) -1,parsefloat (day)); //) get the Year by getFullYear () to avoid the millennium bug problem if (Temp_date.getfullyear ()!=parsefloat ( Year) | | Temp_date.getmonth ()!=parsefloat (month) -1 | | Temp_date.getdate ()!=parsefloat (day)) { return false; }else{ return true; } } nbsp * Verify that the birthdays in the 15-digit ID number are valid birthdays * @param idCard15 15-digit ID card string * @return */ function Isvaliditybrithby15idcard (IDCARD15) { var year = idcard15.substring (6,8); var month = idcard15.substring ( 8,10); var day = idcard15.substring (10,12); var temp_date = new Date (year,parsefloat (month) -1,parsefloat ( Day); //For the age of your old ID card, you do not need to consider the millennium bug problem and use the getyear () method if (Temp_date.getyear ()!=parsefloat (year) | | Temp_date.getmonth ()!=parsefloat (month) -1 | | Temp_date.getdate ()!=parsefloat (day)) { return false; }else{ return true;  } } //Remove string and tail space function trim (str) { return str.replace (/(^s*) | ( s*$)/g, ""); } Update version two code as follows: function Checkidcard (num) { num = num.touppercase () ; //ID number is 15 digits or 18 digits, 15 digits are all digits, 18 digits before 17 digits, and the last is a check digit, possibly a number or a character X. if (!) ( /(^d{15}$) | (^d{17} ([0-9]| X) $)/.test (num)) { //alert (' ID card length is not correct, 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; } //check digit is generated according to ISO 7064:1983.mod 11-2, X can be considered as Number 10. //below analyze birth date and check bit 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 that 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)); nbSp if (!bgoodday) { //alert (' entered the ID number of the birth date is not correct! '); return false; } else { //convert 15 ID card to 18-bit //check digit is generated according to ISO 7064:1983.mod 11-2 X can be thought of as the number 10. var arrint = new Array (7, 9, 5, 8, 4, 2, 1, 6, 3, 7, 9, 5); var arrch = new Array (' 8 ', ' 4 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 '); var ntemp = 0, i; num = num.substr (0, 6) + ' ' + num.substr (6, num.length-6); for (i = 0; i < i + +) { ntemp + = Num.substr (i, 1) * arrint[i]; } num + + arrch[ntemp% 11]; return true; } } if (len =) { re = new RegExp (/^ (D{6}) (D{4}) (D{2}) (D{2}) (D{3}) ([0-9]| X) $/); var arrsplit = Num.match (re); //Check the birthday date is correct var Dtmbirth = new Date (arrsplit[2] + "/" + arr SPLIT[3] + "/" + arrsplit[4]); var bgoodday; bgoodday = (dtmbirth.getfullyear () = number (arrsplit[2)) && Amp ((Dtmbirth.getmonth () + 1) = = number (Arrsplit[3]) &&Amp (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 18-bit ID card is correct. //check digit is generated according to ISO 7064:1983.mod 11-2, X can be considered as Number 10. var valnum; var arrint = new Array (7, 9, 5, 8, 4, 2, 1, 6, 3, 7, 9, 5, 8, 4, 2); var arrch = NE W Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 '); var ntemp = 0, i; for (i = 0; i < i + +) & nbsp { ntemp + + num.substr (i, 1) * arrint[i]; } valnum = arrch[ntemp% 11]; if (valnum!= num.substr (17 , 1) { //alert (' 18-bit ID card check code is not correct! should be: ' + valnum '; return false; } return true; } } return false; }