Verification of ID numbers is a frequent requirement in daily projects. This article lists several points that need to be considered in verification based on relevant standards and personal experience, and finally gives the TypeScript implementation code.
Standard id Card Number
Tracing back to the source is the fundamental method to solve problems. On the national standard information public service platform, I found the standard: GB 11643-1999: citizenship number.
The standard expression of the id card number structure is as follows:
The citizen id card number is a characteristic combination code, which consists of seventeen digit body code and one digit check code. From left to right: six-digit address code, eight-digit date of birth code, three-digit sequence code and one-digit check code.
For example, if the id number of a female citizen is 11010519491231002X, it means the following:
Address code | Date of birth code | The order code | Check code |
---|---|---|---|
110105 | 19491231 | 002 | X |
Among them:
Address code
Refers to the administrative division code of the county (city, flag, district) where the id card holder’s household is located, and its standard is GB/T 2260-2007: The administrative division code of the People’s Republic of China.
According to the standard, China’s administrative division code is a three-layer six-digit structure:
- The first layer:The front twoIs province, autonomous region, municipality directly under the Central Government, and special administrative region. The value can be:
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
, including11 ~ 65 为 Beijing to XinjiangThe coding,71, 81, 82Respectively,Taiwan,Hong Kong,MacauCoding. - The second layer: the middle two characters represent cities, regions, autonomous prefectures, leagues, etc.
- The third layer: the last two, representing counties, autonomous counties, county-level cities, flags, etc.
Date of birth code
GB/T 7408-2005: Data unit and Exchange format Information exchange date and time representation.
In short, there is no separator between the year, month and day. The year is a four-digit number, and the month and day are two-digit numbers.
The order code
Refers to the serial number of 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 men and the even number to women.
My id number is 531, which means at least 264 other people were born on the day I was born, and it’s an odd number, which means I’m a boy.
Check code
The verification code adopts ISO 7064:1983. Mod11-2 standard.
The calculation process is as follows:
1. Weighted sum of the first 17 ontology codes
S = Sum(Ai * Wi)
,i = 0, ..., 16
Ai: indicates the id card number in position I
Wi: represents the weighted factor at the ith position, Wi = 7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2
2. Computing mode
Y = mod(S, 11)
3. Obtain the corresponding verification code through the module
Modulus Y: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Check code: 1, 0, X, 9, 8, 7, 6, 5, 4, 3, 2
For example, if the modulus is 2, the verification code is X.
Validation protocols
With the above instructions, you can easily write out a verification scheme for your ID number.
- An 18-bit character string with a non-0 first digit
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, X
characters - The first two area codes are in
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
Among the - Date of birth is valid and less than the current date
- The result of the check code calculation on the first 17 digits is equal to the character on the 18th digit
In addition, some special cases should be considered:
- Compatible with 15-digit ID card number
- X is case-compatible with X
Typescript-based implementation
With the scheme, the implementation is simple. NPM I vtils, import {isChineseIDCardNumber} from ‘vtils’.
const testRegExp = /^[1-9]([0-9]{14}|[0-9]{16}[0-9Xx])$/ const areaMap = [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] const weightMap = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2] const codeMap = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'] const isValidDate = (year: number, month: number, day: number): boolean => { const date = new Date(year, month - 1, day) return ( date.getFullYear() === year && date.getMonth() + 1 === month && date.getDate() === day && date.getTime() < New Date().getTime())} /** * check if 'value' is a Chinese id number. Value to detect the value of the * * * @ param @ returns is (true) or not (false) * * @ @ see https://my.oschina.net/labrusca/blog/306116's http://developer.51cto.com/art/201803/568755.htm */ export default function isChineseIDCardNumber(value: string): Boolean {const len = value.length // length error if (len! == 15 && len ! == 18) {return false} Testregexp. test(value)) {return false} if (areamap.indexof (+value.substr(0, If (len === 15) {return isValidDate(+ '19${value.substr(6, 2)}', + Value. substr(8, 3)) === -1) {return false} // 15 bit if (len === 15) {return isValidDate(+ '19${value.substr(6, 2)}', + Value. substr(8, 4) 2), +value.substr(10, 2))} // if (! isValidDate(+value.substr(6, 4), +value.substr(10, 2), +value.substr(12, // Const sum = value.split('').slice(0, 17).reduce((s, num, index) => { return s += +num * weightMap[index] }, 0) return codeMap[sum % 11] === value[17].toUpperCase() }Copy the code