varWi = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1];//weighting FactorvarValidecode = [1, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2];//ID Verification bit value. 10 for xfunctionCheckidcard (idcard) {Idcard= Trim (Idcard);//Remove string kinsoku and trailing spaces if(Idcard.length = = 15) { returnIsvaliditybrithby15idcard (Idcard);//verification of 15-digit ID card}Else if(Idcard.length = = 18) { varA_idcard = Idcard.split ("");//get an array of ID cards if(Isvaliditybrithby18idcard (Idcard) &&istruevalidatecodeby18idcard (A_idcard)) {//basic verification and 18th bit verification for 18-bit ID cards return true; }Else { return false; } } Else { return false; } } /** * Determine if the last verification bit is correct when the ID number is 18 digits * @param a_idcard ID number array * @return*/ functionIstruevalidatecodeby18idcard (a_idcard) {varsum = 0;//declaring weighted summation variables if(a_idcard[17].tolowercase () = = ' X ') {a_idcard[17] = 10;//replace the last X-digit verification code with 10 to facilitate subsequent operations } for(vari = 0; I < 17; i++) {sum+ = wi[i] * A_idcard[i];//weighted sum} valcodeposition= sum% 11;//get the Verification code location if(a_idcard[17] = =Validecode[valcodeposition]) { return true; } Else { return false; } } /** * Verify that birthdays in 18-digit ID numbers are valid birthdays * @param idcard 18-bit book ID string * @return*/ functionIsvaliditybrithby18idcard (idCard18) {varYear = idcard18.substring (6,10); varmonth = idcard18.substring (10,12); varDay = idcard18.substring (12,14); varTemp_date =NewDate (year,parsefloat (month)-1, Parsefloat (day)); //get the Year with 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; } } /** * Verify that birthdays in 15-digit ID numbers are valid birthdays * @param idCard15 15-bit book ID string * @return*/ functionIsvaliditybrithby15idcard (idCard15) {varYear = idcard15.substring (6,8); varmonth = idcard15.substring (8,10); varDay = idcard15.substring (10,12); varTemp_date =NewDate (year,parsefloat (month)-1, Parsefloat (day)); //Use the GetYear () method for the age of your old ID card without considering the millennium bug problem if(Temp_date.getyear ()! =parsefloat (year)|| Temp_date.getmonth ()!=parsefloat (month)-1 | | Temp_date.getdate ()! =parsefloat (Day)) { return false; }Else{ return true; } } //Remove string kinsoku and trailing spacesfunctionTrim (str) {returnStr.replace (/(^\s*) | ( \s*$)/g, ""); }
JS-based ID card verification (full version)