Js implementation of the second generation id card number verification details

  • 2020-03-30 04:20:52
  • OfStack

Nonsense less said, directly on the code, the comments of the analysis is very clear, here is not BB.


/*
According to the national standard of the People's Republic of China GB 11643-1999 The provisions regarding the citizenship number in the], the citizenship number is the characteristic combination code, which is composed of seventeen digit main body code and one digit check code. The order is from left to right: six-digit address code, eight-digit date of birth code, three-digit sequence code and one-digit check code.
  Address code represents the county where the encoding object's permanent residence is located ( City, flag, district ) Administrative code.
  The date of birth code represents the year, month, and day of the birth of the encoding object.
  The sequence code refers to the sequence number of persons born in the same year, month and day within the region identified by the same address code. Odd Numbers of sequence codes are given to men and even Numbers to women.
  Check code is based on the preceding seventeen digit code, according to ISO 7064:1983.MOD 11-2 Check code the check code calculated.
 
Birth date calculation method.
The id number of bits first extends the year of birth to 4 Bits, simply add one 19 or 18, So that covers everything 1800-1999 Born in 1977 ;
Those born in the year after next must be 18 Bit of did not have this trouble, as for 1800 previously-born , That what at that time should have no id number this east east, ⊙ b Khan, ...
Here are the regular expressions :
  Date of birth 1800-2099 (18|19|20)?d{2}(0[1-9]|1[12])(0[1-9]|[12]d|3[01])
  Id 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 check rule 6 Bit address coding +6 Date of birth +3 A serial number
Bit check rule 6 Bit address coding +8 Date of birth +3 A serial number +1 position Check bit
 
Check bit rule The formula : ∑ (ai x Wi)(mod 11) ..................................................................... (1)
The formula (1) In:
i---- Represents the position sequence number of the number character from left to left, including the check code;
ai---- According to the first i The number character value on the position;
Wi---- In the first i The weighted factor at the position, whose value is based on the formula Wi=2^(n-1 ) (mod 11) And we can calculate that.
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
 
*/
//Authentication of id number
//Supports 15 - and 18-bit id Numbers
//Support address encoding, date of birth, checkbit validation
function IdentityCodeValid(code) {
var city={11:" Beijing ",12:" tianjin ",13:" hebei ",14:" shanxi ",15:" Inner Mongolia ",21:" liaoning ",22:" Ji Lin ",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:" 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 = " Wrong id number format ";
pass = false;
 }  else if(!city[code.substr(0,2)]){
 tip = " Address coding error ";
pass = false;
 }
 else{
 //The 18-bit id card needs to verify the last bit
if(code.length == 18){
 code = code.split('');
 // ∑ (ai x 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 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);


Related articles: