Home > Article > Web Front-end > JavaScript implements ID card verification code_javascript skills
The meaning of each 18-digit ID number
1-2 digit province, autonomous region, or municipality code;
3-4 digit prefecture-level city, league, autonomous prefecture code;
5-6 digit county, county-level city, district code;
7-14 digits of birth date, for example, 19670401 represents April 1, 1967;
Digits 15-17 are sequence numbers, of which 17 digits are odd numbers for men and even numbers for women;
18 digits are the check code, 0-9 and X, randomly generated by the formula;
Example:
340523 1980 0101 0013 The meaning of this ID number:
34 is Anhui Province
05 is Ma’anshan City
23 is He County
19800101 is the date of birth (January 1, 1980)
001 is the sequence number (1 is an odd number, representing male)
3 is the verification code
Administrative division code
Latest administrative division codes for counties and above (as of October 31, 2014)
Beijing City (110000 BJ)
Tianjin City (120000 TJ)
Hebei Province (130000 HE)
Shanxi Province (140000 SX)
Inner Mongolia Autonomous Region (150000 NM)
Liaoning Province (210000 LN)
Jilin Province (220000 JL)
Heilongjiang Province (230000 HL)
Shanghai (310000 SH)
Jiangsu Province (320000 JS)
Zhejiang Province (330000 ZJ)
Anhui Province (340000 AH)
Fujian Province (350000 FJ)
Jiangxi Province (360000 JX)
Shandong Province (370000 SD)
Henan Province (410000 HA)
Hubei Province (420000 HB)
Hunan Province (430000 HN)
Guangdong Province (440000 GD)
Guangxi Zhuang Autonomous Region (450000 GX)
Hainan Province (460000 HI)
Chongqing City (500000 CQ)
Sichuan Province (510000 SC)
Guizhou Province (520000 GZ)
Yunnan Province (530000 YN)
Tibet Autonomous Region (540000 XZ)
Shaanxi Province (610000 SN)
Gansu Province (620000 GS)
Qinghai Province (630000 QH)
Ningxia Hui Autonomous Region (640000 NX)
Xinjiang Uygur Autonomous Region (650000 XJ)
Taiwan Province (710000 Tw)
Hong Kong SAR (810000 HK)
Macau SAR (820000 Mo)
Calculation method of the 18th digit of ID card (check code)
Multiply the first 17 digits of the ID number by different coefficients;
The coefficients from the first to the seventeenth position are: 7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2
Add the results of multiplying these 17-digit numbers and coefficients;
Divide the added sum by 11 to see what the remainder is;
The remainder can only be 11 numbers: 0-1-2-3-4-5-6-7-8-9-10;
The corresponding last ID number is 1-0-X-9-8-7-6-5-4-3-2
From the above, we know that if the remainder is 2, the Roman numeral X will appear on the 18th digit of the ID card. If the remainder is 10, the last number on the ID card is 2.
Example:
A certain man's ID number is 340523198001010013. We need to see if this ID card is a legal ID card.
First we get the sum of the products of the first 17 digits:
(3*7+4*9+0*10+5*5+2*8+3*4+1*2+9*1+8*6+0*3+0*7+1*9+0 *10+1*5+0*8+0*4+1*2) = 185
Then ask for the remainder:
185 % 11 = 9
Finally, through the corresponding rules, we can know that the number corresponding to the remainder 9 is 3. Therefore, it can be determined that this is a qualified ID number.
JavaScript to verify 18-digit ID card
First check whether the number of digits is 18 digits:
if(!/^\d{17}(\d|x)$/i.test(ID)) return false; // \d 匹配数字 // ^ 匹配开始 // $ 匹配结尾 // i 不区分大小写 // {17} 匹配17次 // \d|x 匹配数字或x
Then check whether the first two digits are legal provinces (municipalities/autonomous regions):
if(city[ID.substr(0,2)] === undefined) return "非法地区";
// The stringObject.substr(start,length) method can extract the specified number of characters starting from the start subscript in the string
// In addition to using dot (.) syntax when accessing object properties, you can also use square brackets ([]). Using square brackets is more flexible
Then check whether the date of birth is legal:
var birthday = ID.substr(6, 4) + '/' + Number(ID.substr(10, 2)) + '/' + Number(ID.substr(12, 2)); var d = new Date(birthday); var newBirthday = d.getFullYear() + '/' + Number(d.getMonth() + 1) + '/' + Number(d.getDate()); var currentTime = new Date().getTime(); var time = d.getTime(); if(time >= currentTime || birthday !== newBirthday) return '非法生日'; // 获取身份证的年月日,然后再 new 一个 Date,再对比这两个日期是否一致 // 这里用Number()主要是因为身份证的日期是带0的,而new Date()出来的日期是不带0的,Number()之后就都不带0了
Finally determine whether the check code is correct:
var arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; var arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']; var sum = 0, i, residue; for(i=0; i<17; i++) { sum += ID.substr(i, 1) * arrInt[i]; } residue = arrCh[sum % 11]; if (residue !== ID.substr(17, 1)) '非法证号';
If the above verification is passed, it is a legal ID number;
Full code
<script> function checkID(ID) { if(typeof ID !== 'string') return '非法字符串'; var city = {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:"澳门",91:"国外"}; var birthday = ID.substr(6, 4) + '/' + Number(ID.substr(10, 2)) + '/' + Number(ID.substr(12, 2)); var d = new Date(birthday); var newBirthday = d.getFullYear() + '/' + Number(d.getMonth() + 1) + '/' + Number(d.getDate()); var currentTime = new Date().getTime(); var time = d.getTime(); var arrInt = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2]; var arrCh = ['1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2']; var sum = 0, i, residue; if(!/^\d{17}(\d|x)$/i.test(ID)) return '非法身份证'; if(city[ID.substr(0,2)] === undefined) return "非法地区"; if(time >= currentTime || birthday !== newBirthday) return '非法生日'; for(i=0; i<17; i++) { sum += ID.substr(i, 1) * arrInt[i]; } residue = arrCh[sum % 11]; if (residue !== ID.substr(17, 1)) return '非法身份证哦'; return city[ID.substr(0,2)]+","+birthday+","+(ID.substr(16,1)%2?" 男":"女") } </script>