How to filter ID numbers with regular expressions

What I brought to you this time is how to filter ID numbers with regular expressions. What are the precautions for using regular expressions to filter ID numbers? The following are actual combat cases. Let's have a look.

A brief remark

The regular expression of ID number and verification scheme are often used when verifying users' real names. This paper lists two verification schemes, and you can choose the appropriate scheme according to your actual project situation.

ID number description

The correct and formal title of resident identity card number should be "citizen identity number". According to the national standard GB11643-1999 of People's Republic of China (PRC), the citizenship number is a characteristic combination code, which consists of a 17 digit digital ontology code and a digital check code. The order from left to right is: six-digit address code, eight-digit date of birth code, three-digit serial code and one-digit check code.

Take a female ID number in Chaoyang District, Beijing as an example. The meaning of ID number is shown in the following figure:

Note: The ID number comes from GB11643-1999.

Let's complete a complete ID number verification process from scratch.

Scheme 1 (simple)

1. 1 partition rules

1. 1. 1 address code rule: the length of the address code is 6 digits.

Start with the number 1-9.

The last five numbers are numbers from 0 to 9.

According to the above rules, write the regular expression of the address code: /[ 1-9] \ d {5}/

1. 1.2 year code rule: the year code is 4 digits long.

Start with the number 18, 19 or 20.

The remaining two digits are 0-9.

According to the above rules, write the regular expression of year coding:/(18 |19 | 20) \ d {2}/. If the year starting with 18 is not needed, 18 can be removed.

1. 1.3 code rules:

The month code is 2 digits.

The first digit is 0, and the second digit is 1-9.

Or the first bit is 1 and the second bit is 0-2.

According to the above rules, write the regular expression of the month code:/((0 [1-9]) | (1[0-2])/.

1. 1.4 date code rules:

The date code is 2 digits.

The first digit is 0-2, and the second digit is 1-9.

Or10,20,30,31.

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

1. 1.5 sequence code rules:

The serial code is 3 digits long.

The serial code is a number.

According to the above rules, write the regular expression of sequence code: /\d{3}/.

1. 1.6 check code rules:

The length of the check code is 1 bit.

It can be a number, the letter x, or the letter X.

According to the above rules, write the regular expression of the check code: /[0-9Xx]/.

1.2 scheme 1 regular expression

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|3 1)\d{3}[0-9xx]$/;

//output true

console . log(p . test(" 1 10 105 1949 123 1002 x "));

//Output false cannot start with 0.

console . log(p . test(" 0 10 105 1949 123 1002 x "));

//The output fake year cannot start with 17.

console . log(p . test(" 1 10 105 1749 123 1002 x "));

//The output fake month cannot be 13.

console . log(p . test(" 1 10 105 1949 133 1002 x "));

//The output fake date cannot be 32.

console . log(p . test(" 1 10 105 1949 1232002 x "));

//Output false cannot end in.

console . log(p . test(" 1 10 105 1949 1232002 a ")); 1.3 scheme analysis

Scheme 1 only makes a basic format judgment, which mainly has three shortcomings:

Address code determination is not accurate enough. Example: There are no areas in China that begin with16,26, but it can be judged that it is not accurate enough by checking the date. Example: 1949023 1 can also be verified, but there is no 3 1 parity code in February, which is calculated with 17 bit ontology code. Scheme 1 does not verify this code scheme 2 (comprehensive).

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

2. 1 province address code check

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

Northeast China: Liaoning 2 1, Jilin 22, Heilongjiang 23.

East China: Shanghai 3 1, Jiangsu 32, Zhejiang 33, Anhui 34, Fujian 35, Jiangxi 36 and Shandong 37.

Central China: Henan 4 1, Hubei 42, Hunan 43.

South China: Guangdong 44, Guangxi 45, Hainan 46

Southwest: Sichuan 5 1, Guizhou 52, Yunnan 53, Tibet 54, Chongqing 50.

Northwest: Shaanxi 6 1, Gansu 62, Qinghai 63, Ningxia 64, Xinjiang 65.

Special: Taiwan Province Province 7 1, Hong Kong 8 1, Macau 82.

According to the above address code, check the first two digits of the ID number to further improve the accuracy. The current address code is based on the 20 13 version of the administrative division code GB/T2260. Due to the historical evolution of the zoning code, the last four digits of the address code cannot be checked. Take Sanpang's ID number as an example. My number starts with 232 1, but there is no such code in the current administrative division code list. Therefore, this paper only checks the first two provincial address codes.

It is also said that 9 1 starts with the first two digits of the Chinese ID card number obtained by foreigners, but I have not been confirmed. If you have an ID card starting with 9 1 or know Marbury, please help confirm the relevant information.

According to the above analysis, the verification and testing procedures of provincial address codes are as follows:

var checkProv = function (val) {

Var mode =/[1-9] [0-9]/;

Var provs = {1 1: "Beijing", 12: "Tianjin", 13: "Hebei", 14: "Shanxi", 15: " 33: Zhejiang, 34: Anhui, 35: Fujian, 36: Jiangxi, 37: Shandong, 4 1: Henan, 42: Hubei, 43: Hunan, 44: Guangdong. 6 1: Shaanxi, 62: Gansu, 63: Qinghai, 64: Ningxia, 65: Xinjiang, 7 1: Taiwan Province Province, 8 1: Hong Kong, 82.

if(pattern.test(val)) {

if(provs[val]) {

Return true

}

}

Returns false

}

//Output true, 37 is Shandong.

console . log(check prov(37));

//Output false, 16 does not exist.

console . log(check prov( 16)); 2.2 Date of birth code verification

Without explaining the verification of birth date code, the following functions and test steps are given directly:

var checkDate = function (val) {

Var mode =/(18 |19 | 20) \ d {2} ((0 [1-9]) | (1[0-2]) ([0-2])

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 (date & date2.getmonth () = = (parse int (month)-1)) {

Return true

}

}

Returns false

}

//output true

console . log(checkDate(" 20 1802 12 "));

//Output false There is no 3 1 day in February.

console . log(checkDate(" 20 18023 1 ")); 2.3 Check the check code

The calculation of check code is slightly complicated, so the following formula is given first:

Where ai represents the i-th value of the ID card body code and Wi represents the i-th weighting factor value.

Weight coefficient table 1:

I 12345678 wi 79 105842 19 10 1 13 14 14 14 15 1565438。

X 012345678910a1810x98765432 algorithm flow:

According to the ID card account code (front 17 digits) and the corresponding weighting factor (table 1), the product is calculated and then summed, and the obtained result is modulo 1 1 to get the x value.

Look up table 2 according to the x value and get a 18, that is, the check code value.

Check the code calculation program and test it. See the following code:

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|3 1)\d{3}[0-9xx]$/;

Var factor = [7,9, 1 0,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 % 1 1]= = code . toupper case()){

Return true

}

}

Returns false

}

//Output true, and the check code matches.

console . log(checkCode(" 1 10 105 1949 123 1002 x "));

//Output false, and the check code does not match.

console . log(checkCode(" 1 10 105 1949 123 1002 1 ")); 2.4 Scheme II Overall Code

var checkID = function (val) {

if(checkCode(val)) {

var date = val.substring(6, 14);

If (inspection date (date))

if(checkProv(val.substring(0,2))) {

Return true

}

}

}

Returns false

}

//output true

console . log(checkID(" 1 10 105 1949 123 1002 x "));

//Output false, and the check code does not match.

console . log(checkID(" 1 10 105 1949 123 1002 1 "));

//Output false, date code does not match.

console . log(checkID(" 1 10 105 1949023 10026 "));

//Output false, and the area code does not match.

console . log(checkID(" 160 105 1949 123 10029 ")); I believe you have mastered the method after reading this case. For more exciting, please pay attention to other related articles on Gxl!

Recommended reading:

Vue.js customizes the way events enter the form.

How does Vue.js make pictures drag at will?

How to cross-domain and render using vue2.0axios?