Less nonsense, just put the code directly, the parsing in the comments is very clear, so there is no BB here.
The code copy is as follows:
/*
According to the provisions on citizenship identity numbers in the National Standard GB 11643-1999 of the People's Republic of China, the citizenship identity number is a characteristic combination code, consisting of a seventeen-digit digital ontology code and a one-digit verification code. The arrangement order is from left to right: six-digit address code, eight-digit date of birth code, three-digit sequence code and one-digit verification code.
The address code represents the administrative division code of the county (city, banner, and district) where the permanent residence of the coded object is located.
The date of birth code represents the year, month and day of the encoded object's birth, and the year is represented by four digits, and there is no separator between the year, month and day.
The sequence code represents the sequence number assigned to persons born in the same year, month, and day within the area identified by the same address code. The odd numbers of the order codes are assigned to men and even numbers are assigned to women.
The verification code is a verification code calculated based on the first seventeen digit codes and according to the verification code of ISO 7064:1983.MOD 11-2.
Calculation method of date of birth.
The ID card code of the bit first expands the year of birth to 4 bits, simply adding a 19 or 18, which includes all people born between 1800 and 1999;
Those born after the New Year must be 18, and there is no such worry. As for those born 1,800 years ago, there should be no ID number at that time, ⊙⊙b Khan...
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
Bit verification rules 6-digit address code + 6-digit date of birth + 3-digit sequence number
Bit verification rules 6-bit address code + 8-bit date of birth + 3-bit sequence number + 1-bit verification bit
Verification digit rule formula: ∑(ai×Wi)(mod 11)………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………………
In formula (1):
i----- indicates the number sequence number of the number character from the left including the verification code;
ai---- represents the number character value at the i-th position;
Wi-----Shows the weighting factor at the i-th position, and its value is calculated based on 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 the legality of the ID number
//Support 15-digit and 18-digit ID numbers
//Support address encoding, date of birth, and verification of check digits
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:"Macao",91:"Foreign"};
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 = "Identity card number format error";
pass = false;
}
else if(!city[code.substr(0,2)]){
tip = "Address encoding error";
pass = false;
}
else{
//The 18-digit ID card needs to be verified for the last check bit
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 ];
//Calibration bit
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 bit error";
pass =false;
}
}
}
if(!pass) alert(tip);
return pass;
}
var c = '130981199312253466';
var res= IdentityCodeValid(c);
How about it? It’s a very comprehensive first ID number verification code. It’s not the same dimension as the other verification codes searched online. Please take it away if you need it.