Stop talking nonsense and go directly to the code. The analysis is very clear in the comments, so there will be no BB here.
/*
According to the regulations on citizen identity numbers in the National Standard of the People's Republic of China GB 11643-1999, the citizen identity number is a characteristic combination code, consisting of a seventeen-digit body code and a one-digit check code. The order from left to right is: six-digit address code, eight-digit date of birth code, three-digit sequence code and one-digit check code.
The address code represents the administrative division code of the county (city, banner, district) where the coding object’s permanent residence is located.
The date of birth code represents the year, month, and day of the encoding object's birth, where the year is represented by four digits, and no separators are used between the year, month, and day.
The sequence code indicates the sequence number assigned to persons born in the same year, month and day within the area identified by the same address code. Odd numbers in the sequence code are assigned to men, and even numbers are assigned to women.
The check code is a check code calculated based on the previous seventeen-digit code and in accordance with the ISO 7064:1983.MOD 11-2 check code.
How to calculate date of birth.
The ID card code of digits first extends the year of birth to 4 digits. Simply add a 19 or 18, so that all people born between 1800 and 1999 are included;
Those born after 1800 must be in the 18th digit and don’t have this problem. As for those born before 1800, they probably didn’t have an ID number at that time, ⊙﹏⊙bhan...
The following is the regular expression:
Date of birth 1800-2099 (18|19|20)?d{2}(0[1-9]|1[12])(0[1-9]|[12]d|3[01])
ID card regular expression/^d{6}(18|19|20)?d{2}(0[1-9]|1[12])(0[1-9]|[12]d|3 [01])d{3}(d|X)$/i
Digit verification rules 6-digit address code 6-digit date of birth 3-digit sequence number
Bit verification rules: 6-digit address code, 8-digit date of birth, 3-digit sequence number, 1-digit check digit
Check digit rule formula: ∑(ai×Wi)(mod 11)…………………………………………(1)
In formula (1):
i---- represents the position sequence number of the number characters from left to right including the check code;
ai---- represents the number character value at the i-th position;
Wi---- represents the weighting factor at the i-th position, and its value is calculated according to the formula Wi=2^(n-1) (mod 11).
i 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
Wi 7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4 2 1
*/
//Verification of legality of ID number
//Supports 15-digit and 18-digit ID numbers
//Support address coding, date of birth, check digit verification
function IdentityCodeValid(code) {
var city={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:"Macau",91:"Overseas"};
var tip = "";
var pass= true;
if(!code || !/^d{6}(18|19|20)?d{2}(0[1-9]|1[12])(0[1-9]|[ 12]d|3[01])d{3}(d|X)$/i.test(code)){
tip = "ID card number format is wrong";
pass = false;
}
else if(!city[code.substr(0,2)]){
tip = "Incorrect address encoding";
pass = false;
}
else{
//18-digit ID card needs to verify the last check digit
if(code.length == 18){
code = code.split('');
//∑(ai×Wi)(mod 11)
//Weighting factor
var factor = [ 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2 ];
//Check digit
var parity = [ 1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2 ];
var sum = 0;
var ai = 0;
var wi = 0;
for (var i = 0; i < 17; i )
{
ai = code[i];
wi = factor[i];
sum = ai * wi;
}
var last = parity[sum % 11];
if(parity[sum % 11] != code[17]){
tip = "Check digit error";
pass =false;
}
}
}
if(!pass) alert(tip);
return pass;
}
var c = '130981199312253466';
var res= IdentityCodeValid(c);
How about it? It is a very comprehensive first ID number verification code. It is simply not the same dimension as other verification codes found online. Friends, just take it and use it if you need it.