The following is the code based on the ID number encoding rules, using JS to validate it
<script type= "Text/javascript" >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 verification bit value. 10 represents X function Idcardvalidate (idcard) {Idcard = Trim (idcard.replace (//g, "")); Remove String Kinsoku if (idcard.length = =) {return Isvaliditybrithby15idcard (Idcard); Authentication for 15-bit ID} else if (idcard.length = =) {var A_idcard = Idcard.split (""); Get ID Array if (isvaliditybrithby18idcard (Idcard) &&istruevalidatecodeby18idcard (A_idcard)) {//base for 18-bit ID This verification and the verification of the 18th bit 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 */function Istruevalidatecodeby18idcard (A_idcard) {var sum = 0; Declares a weighted sum variable if (a_idCard[17].tolowercase () = = ' x ') {a_idcard[17] = 10; Replace the last verification code of X with 10 to facilitate subsequent operation} for (var i = 0; i < 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 */function ISVALIDITYBRITHBY18IDCA RD (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 here 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 */function Isvaliditybri Thby15idcard (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 old ID, 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 Kinsoku function trim (str) {return str.replace (/(^\s*) | ( \s*$)/g, ""); } </script>
Online Demo ← Don't believe you try
Itmyhome
JS strictly check the ID number