simply says

often uses the regular expression and checkout scheme of the identity card number when it is used to verify the real name of the user. This article enumerates two verification schemes, and we can choose the suitable scheme according to the actual situation of the project.

idcard-check

ID number indicates

resident ID card number. The correct and formal appellation should be "citizenship number". According to People's Republic of China national standard GB 11643-1999, the identity number is a combination code, which is composed of seventeen digit ontology code and one digit check code. The order from left to right is six digit address code, eight digit date code, three digit sequence code and one digit check code.

in Beijing city of Chaoyang District a female identity card number, ID number meaning as shown below:

ID number meanings of

note: the ID number from the national standard [GB 11643-1999]. Under

, we complete a complete ID number verification process from zero.

1 (simple)

1.1

1.1.1 code division rules rules:
address code length 6
start with the number 1-9
after 5 to 0-9 the number of

according to the above rules, write regular expressions address code: /^[1-9]d{5}/

1.1.2 year code rules:
year code length of 4 bits in digital
18, 19 or 20 at the beginning of two to 0-9 of the remaining
digital

according to the above rules, write regular expressions year Codes: / d{2}/ (18 |19|20). If you do not need the 18 beginning of the year, you can remove 18.

1.1.3 in the code rules:
month code length 2
the first digit is 0, second digits for 1-9
or the first digit is 1, second digit 0-2

according to the above rules, write regular expressions in the code: / ((0[1-9]) | (1[0-2])) /.

1.1.4 date code rule:
date code length 2
the first digit is 0-2, second digits is 1-9
or 10, 20, 30, 31

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

1.1.5 sequence code rule:
order code length, 3 bit
sequence code is digital

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

1.1.6 check code rule:
check code length 1 bit
can be number, letter X or letter X

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

1.2

scheme 1 regular expressions above 6 rules, regular expressions and test procedures are as follows:

 var P = /^[1-9]d{5} (18|19|20) d{2} ((0[1-9]) | (1[0-2]) (|10|20|30|31) ([0-2][1-9])) d{3}[0-9Xx]$/; / / true console.log output (p.test ("11010519491231002X")) false; / / output can not begin with 0 console.log (p.test ("01010519491231002X")); / / output false year can not be at the beginning of 17 console.log (p.test ("11010517491231002X")); / / output false month can not be 13 console.log (p.test ("11010519491331002X")); / / false output date cannot be 32 console.log (p.test (the "11010519491232002X")); / / output false cannot end with a console.log (p.test ("11010519491232002a")); 

1.3 1

1 program analysis program Only a basic format decision has been done, and there are three main shortcomings: the
address code is not accurate enough. In our country, there is no area of 16, 26, but it can be judged inaccurate by the date of verification. Example: 19490231, it can also be verified. In February, there was no 31 day parity check code calculated by 17 bit ontology code. Scheme 1 did not verify the code 2 (comprehensive)
, according to the lack of scheme 1, the scheme 2 was introduced to further improve the scheme 1.

2.1

North China Provincial code check address: 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< br>: Henan 41, Hubei 42, China 43
Southern China, Hunan: Guangdong 44, Guangxi 45, Hainan 46
Southwest: Sichuan 51, Guizhou 52, Yunnan 53, Tibet 54, Chongqing 50
Northwest: Shaanxi 61, Gansu 62, Qinghai 63, Ningxia 64, Xinjiang 65
: Taiwan 71, Hongkong 81, Macao 82

according to the first two check the address code do the identity card number, to further improve the accuracy of. The current address code is based on the 2013 edition of the administrative division code [GB/T2260]. Because of the historical evolution of the zoning code, the four bit check after the address code has become less likely. With three fat ID card number as an example, my number is 2321, and there is no code in the current administrative division code table. Therefore, this paper only does the verification of the first two provincial address codes.

also has a statement that the beginning of 91 is the first two bits of a foreigner to obtain a Chinese identity card number, but I have not been confirmed. If you have 91 first ID cards or know Marbury, please help to confirm the information.
根据以上分析,给出省级地址码校验及测试程序如下:

 var checkProv = function (val) {  var pattern = /^[1-9][0-9]/;  var provs = {11:"北京",12:"天津",13:"河北",14:"山西",15:"内蒙古",21:"辽宁",22:"吉林",23:"黑龙江 ",31:"上海",32:"江苏",33:"浙江",34:"安徽",35:"福建",36:"江西",37:"山东",41:"河南",42:"湖北 ",43:"湖南",44:"广东",45:"广西",46:"海南",50:"重庆",51:"四川",52:"贵州",53:"云南",54:"西藏 ",61:"陕西",62:"甘肃",63:"青海",64:"宁夏",65:"新疆",71:"台湾",81:"香港",82:"澳门"};  if(pattern.test(val)) {   if(provs[val]) {    return true;   }  }  return false; } //输出 true,37是山东 console.log(checkProv(37)); //输出 false,16不 The presence of console.log (checkProv (16)); 

2.2

code check calibration date of birth date of birth code without explanation, direct function and test procedures are given as follows:

 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")); / / false in February 31, no output console.log (checkDate ("20180231")); 

2.3 check codes

校验码的计算略复杂,先给出如下公式:

其中 ai 表示身份证本体码的第 i 位值,而 Wi 表示第 i 位的加权因子值。

weighted factor table [table 1]:

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

and

X 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: according to the ID code subject (the first 17) respectively the corresponding weighting factor (Table 1) to calculate the sum of the product, according to the results obtained with the 11 modulo X value.
  • inquires the table 2 according to the X value, and obtains the A18 as the check code value.

校验码计算程序及测试见如下代码:

 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; } // 输出 true, 校验码相符 console.log(checkCode("11010519491231002X")); // 输出 false, 校验码不符 console.log(checkCode("110105194912310021"));

2.4 方案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 (checkID ("11010519491231002X console.log")); / / output false checksum does not match the console.log (checkID ("110105194912310021")); / / output false, console.log does not match the date code (checkID ("110105194902310026")); / / false code to output, does not match the console.log (checkID ("160105194912310029")); 

for more understanding and analysis of the identity card number verification three fat, if there is insufficient please be.

This paper fixed link:http://www.script-home.com/regular-expression-and-verification-of-javascriptregex-id-card-number.html | Script Home | +Copy Link

Article reprint please specify:Regular expression and verification of JavaScript+Regex ID card number | Script Home

You may also be interested in these articles!