preface
In the application system development, we often meet the problem of correct identification of resident id number. In response to the above problems, @Morrit wrote this article and posted it in class 30, hoping to help future programmers.
This paper briefly discusses the code rules and verification methods of the current citizen ID number in China, and provides JavaScript version and Java version of the verification implementation code, other versions of the code will be added in one after another.
According to the provisions of the current “Law of the People’s Republic of China on Resident Identity Cards” 2011 revised edition, the first-generation id card (15 digits) has ceased to be used since January 1, 2013, and the current 18-digit ID card number is called the second-generation ID card.
The id card number referred to in this article refers to the second generation id card number.
Background knowledge
Id code rules
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.
As can be seen from the figure above, the second-generation ID card number consists of 18 digits: address code (6 digits), date of birth code (8 digits), sequence code (3 digits), and parity code (1 digit).
Address code
It represents the administrative code of the county (city, flag, district) where the encoding object’s permanent residence is located, according to the provisions of GB/T 2260.
Date of birth code
Represents the birth year, month and day of the coding object, according to GB/T 7408. No delimiters are used between year, month, and day codes.
The order code
The sequence code is the serial number of the people born in the same year, month and day within the area identified by the same address code. The odd number of the sequence code is assigned to the male and the even number to the female, that is, the 17th odd number represents the male and the even number represents the female.
Check code
The verification code adopts ISO 7064:1983, MOD 11-2 verification character system. See the next section for specific calculation and verification methods.
GB: GB 11643-1999
Area code rule
In the past, the area code part of the citizenship number does not comply with GB/T 2260. Therefore, the area code verification can only be performed for simple provincial code verification (the first two digits).
Provincial address code:
- 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
’91’ is the first two digits of a foreigner’s Chinese identity number, but the claim has not been confirmed. If you have an ID number starting with 91, please help confirm relevant information.
Date of birth code rule
The verification of birth date code is relatively simple, as long as it conforms to the basic format of date, month limit projection, day limit and age range rules.
The age range needs to be set according to the specific application of the actual threshold range.
Example:
- 20081423 — Month error
- 20080230 — Incorrect date
- 20280203 — Incorrect year
Verification code calculation rules
The character values of the numbers in each position of the citizenship number should meet the verification of the following formula:
Formula 1:18∑ I =1(ai×Wi)≡1(mod11)
- I indicates the sequence number of the number character from right to left, including the check code character.
- Represents the number character value in position I
- Represents the weighting factor at the ith position, and its value is according to the formulaThat’s calculated. Table 1 lists the weighting factors for each position in the citizen id numberNumerical values.
Table 1:
i | 18 | 17 | 16 | 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
7 | 9 | 10 | 5 | 8 | 4 | 2 | 1 | 6 | 3 | 7 | 9 | 10 | 5 | 8 | 4 | 2 | 1 |
Calculation of check code character values
When I =1,, Formula 1 can be expressed as:
Formula 2: a1+18∑ I =2(ai×Wi)≡1(mod11)
In Formula 2,Is the verification code character value, whose value range is; whenThe Roman numeral character is used when the value is equal to 10Said.
Check character values that satisfy formula 2 and value range requirementsAccording to the 与 Is calculated as shown in Table 2.
Table 2:
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | |
---|---|---|---|---|---|---|---|---|---|---|---|
Check code character value | 1 | 0 | X | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 |
JavaScript implementation
JS Verification area code
The basic JavaScript implementation code of provincial address code is 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 province", the 22: "jilin", 23: "in heilongjiang province 32: ", 31: "Shanghai", "jiangsu province", 33: "zhejiang", 34: "anhui", 35: "fujian", 36: "jiangxi", 37: "shandong", 41: "henan", 42: "in hubei province 44:43: ", "hunan", "guangdong", 45: "guangxi", 46: "hainan", 50: "chongqing", 51: "sichuan", 52: "guizhou", 53: "yunnan", 54: "Tibet ", 61: "", 62:" shaanxi 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 no console.log(checkProv(16));Copy the code
You can modify the above program implementation code according to the actual application needs.
JS validates date of birth
The basic JavaScript implementation code for date of birth code is 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")); // print false 2 months no 31 days console.log(checkDate("20180231"));Copy the code
The above code includes 18 years, 19 years, 20 years, you can modify the above program implementation code according to the actual application needs.
JS Verification code
The basic JavaScript implementation code of the verification code is 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 to match console.log(checkCode("11010519491231002X")); Console. log(checkCode("110105194912310021"));Copy the code
JS complete code listing
Verification function:
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; } // print true console.log(checkID("11010519491231002X")); // print false, the verification code is not console.log("110105194912310021")); Console. log(" checkID("110105194902310026")); // print false, the area code does not match console.log(" checkID("160105194912310029"));Copy the code
Sample program:
View sample programs
Java implementation code
Java code list
View sample programs
reference
National Standard: Citizenship Number
National standard: Code of the People’s Republic of China [GB/T 2260-2002]
GB/T 7408-2005 National Standard: Data elements and Exchange formats Date and time representation for information exchange
04 gB standard: Data processing — Check character systems [ISO 7064:1983]
conclusion
The above is the analysis and realization of the correctness of the identification number of @Maorui, the deficiencies of programmers please point out!
Lesson 30