Jane said

Regular expression and verification scheme of ID number are often used in user real name authentication. This article lists two kinds of verification scheme, you can choose the suitable scheme according to their own project actual situation.

Id Card Number

The correct and formal title should be “citizenship number”. According to the national Standard OF the People’s Republic of China (GB 11643-1999), the citizenship number is a combination of feature codes, consisting of seventeen digit ontology codes and one digit verification codes. From left to right: six-digit address code, eight-digit date of birth code, three-digit sequence code and one-digit check code.

Taking a female ID card number in Chaoyang District, Beijing as an example, the meaning of the ID card number is as follows:

Note: The ID card number comes from GB [GB 11643-1999].

Below we start from scratch to complete a complete id number check process.

Option 1 (Simple)

1.1 Branch Rules

We first proposed scheme 1 and made the following rule definition step by step:

1.1.1 Address Code Rules:

  • The address code is 6 digits long
  • Start with the numbers 1-9
  • The last five digits are 0-9

Write the regular expression for each address code according to the above rules: /^[1-9]\d{5}/

1.1.2 Year Code Rules:

  • The year code length is 4 bits
  • Start with the number 18,19 or 20
  • The remaining two digits are 0-9

According to the rules above, write a regular expression: date code/(20) 18 19 | | \ d {2} /. If you do not want a year starting with 18, you can drop 18.

1.1.3 Month Code Rules:

  • Month code length 2 bits
  • The first digit is 0 and the second digit is 1-9
  • Or the first digit is 1 and the second digit is 0-2

According to the above rules and write code in a regular expression: / ((0 [1-9]) | (1) [0-2]) /.

1.1.4 Date Code Rules:

  • The date code is 2 bits long
  • The first digit is 0-2 and the second digit is 1-9
  • Or 10,20,30,31

According to the rules above, write a regular expression: date code/(([0-2] [1-9]) 10 20 | | | | 30 31) /.

1.1.5 Sequence Code Rules:

  • The sequential code length is 3 bits
  • Sequential codes are numbers

Write the regular expression for the sequential code according to the above rules: /\d{3}/.

1.1.6 Verification Code Rules:

  • The verification code length is 1 bit
  • It can be a number, a letter X or a letter X

Based on the preceding rules, write the regular expression for the verification code as /[0-9xx]/.

1.2 Solution 1 Regular Expressions

Based on the above six rules, the complete regular expression and test program are given as follows:

var p = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
/ / output true
console.log(p.test("11010519491231002X"));
// Output false cannot start with 0
console.log(p.test("01010519491231002X"));
// Output false The year cannot start with 17
console.log(p.test("11010517491231002X"));
// Print false the month cannot be 13
console.log(p.test("11010519491331002X"));
// Print false the date cannot be 32
console.log(p.test("11010519491232002X"));
// Output false cannot end with a
console.log(p.test("11010519491232002a"));
Copy the code

Demo code

1.3 Analysis of Scheme 1

Scheme 1 only makes basic format judgment, and has three main deficiencies:

  • Address code judgment is not accurate. Example: There is no area beginning with 16 and 26 in China, but it can be verified
  • Date determination is not accurate enough. Example :19490231 can also be verified, and February does not exist on 31
  • The verification code is calculated by 17-bit ontology code, which is not verified in scheme 1

Option 2 (Comprehensive)

According to the deficiency of scheme 1, scheme 2 is introduced to improve the deficiency of scheme 1.

2.1 Provincial address verification

North China: Beijing 11, Tianjin 12, Hebei 13, Shanxi 14, Inner Mongolia 15

Northeast: Liaoning 21, Jilin 22, Heilongjiang 23

East China: Shanghai 31, Jiangsu 32, Zhejiang 33, Anhui 34, Fujian 35, Jiangxi 36, Shandong 37

Central China: Henan 41, Hubei 42, Hunan 43

South China: Guangdong 44, Guangxi 45, Hainan 46

Southwest China: Sichuan 51, Guizhou 52, Yunnan 53, Xizang 54, Chongqing 50

Northwest: Shaanxi 61, Gansu 62, Qinghai 63, Ningxia 64, Xinjiang 65

Special: Taiwan 71, Hong Kong 81, Macau 82

Verify the first two digits of the ID card number according to the above address code to further improve the accuracy. The current address code is based on the 2013 version of the administrative code [GB/T2260] as the standard. Due to the historical evolution of the region code, it is impossible to verify the last four digits of the address code. Take the ID number of Three fat as an example. His id number starts with 2321, but there is no such code in the current code table of administrative divisions. Therefore, only the first two provincial address codes are verified in this paper.

There is also a claim that statement 91 begins with the first two digits of a foreigner’s Chinese IDENTITY card number, but I have not been confirmed. If you have an ID card starting with 91 or know Stephon Marbury, please help confirm the relevant information.

Based on the above analysis, the provincial address code verification and test program is given as follows:

var checkProv = function (val) {
    var pattern = / ^ (1-9] / [0-9];
    var provs = {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 province".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:"The ningxia".65:"Xinjiang".71:"Taiwan".81:"Hong Kong".82:"Macau"};
    if(pattern.test(val)) {
        if(provs[val]) {
            return true; }}return false;
}
// output true, 37 is shandong
console.log(checkProv(37));
// Print false, 16 does not exist
console.log(checkProv(16));
Copy the code

Demo code

2.2 Verification of birth date code

The verification of date of birth code is not explained, and the following functions and test procedures are given directly:

var checkDate = function (val) {
    var pattern = /^(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)$/;
    if(pattern.test(val)) {
        var year = val.substring(0.4);
        var month = val.substring(4.6);
        var date = val.substring(6.8);
        var date2 = new Date(year+"-"+month+"-"+date);
        if(date2 && date2.getMonth() == (parseInt(month) - 1)) {
            return true; }}return false;
}
/ / output true
console.log(checkDate("20180212"));
// Print false February has no 31
console.log(checkDate("20180231"));
Copy the code

Demo code

2.3 Verification Code Verification

The calculation of verification code is slightly complicated, and the following formula is given first:

Where ai represents the i-th bit value of id card ontology code, and Wi represents the i-th weighted factor value.

Weighting factor table [Table 1] :

i 1 2 3 4 5 6 7 8
Wi 7 9 10 5 8 4 2 1
9 10 11 12 13 14 15 16 17
6 3 7 9 10 5 8 4 2

X and Check Code Conversion Table [Table 2]

X 0 1 2 3 4 5 6 7 8 9 10
a18 1 0 X 9 8 7 6 5 4 3 2

Algorithm process:

  • Calculate the product and sum according to the main id card code (the first 17 bits) and the corresponding weighting factors (Table 1), and modulo the result with 11 to get the X value.
  • According to X value query table 2, a18 is the check code value.

Check code calculation procedures and tests are as follows:

var checkCode = function (val) {
    var p = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
    var factor = [ 7.9.10.5.8.4.2.1.6.3.7.9.10.5.8.4.2 ];
    var parity = [ 1.0.'X'.9.8.7.6.5.4.3.2 ];
    var code = val.substring(17);
    if(p.test(val)) {
        var sum = 0;
        for(var i=0; i<17; i++) { sum += val[i]*factor[i]; }if(parity[sum % 11] == code.toUpperCase()) {
            return true; }}return false;
}
// Print true, the verification code matches
console.log(checkCode("11010519491231002X"));
// Print false, the verification code does not match
console.log(checkCode("110105194912310021"));
Copy the code

Demo code

2.4 Overall code of Scheme 2

var checkID = function (val) {
    if(checkCode(val)) {
        var date = val.substring(6.14);
        if(checkDate(date)) {
            if(checkProv(val.substring(0.2))) {
                return true; }}}return false;
}
/ / output true
console.log(checkID("11010519491231002X"));
// Print false, the verification code does not match
console.log(checkID("110105194912310021"));
// print false, the date code does not match
console.log(checkID("110105194902310026"));
// Print false, the area code does not match
console.log(checkID("160105194912310029"));
Copy the code

Demo code

The above is three fat identification number verification of understanding and analysis, if there is insufficient please give correction.

Renowned physicist Stephen Hawking has died on March 14, 2018, aged 76. A learned and interesting person, all the way!

There is no Hawking in the world, time is a brief history!

The original address