18-digit ID number, what do you mean?
1-2 provinces, autonomous regions, municipalities directly under the code;
3-4 prefecture-level cities, UNITA, autonomous Prefecture code;
5-6 counties, county level, District code;
7-14 date of birth, such as 19670401 delegates April 1, 1967;
15-17-digit sequence number, of which 17 male is singular, female is a double;
The 18-bit is the check code, 0-9 and X, which are randomly generated by the formula;
Example:
340523 1980 0101 0013 The meaning of this ID number:
34 for Anhui Province
05 for Maanshan
23 for Counties
19800101 Date of birth (January 1, 1980)
001 is a sequential number (1 is singular, represented as male)
3 for verification Code
Administrative Division Code
The latest county and county administrative Division code (as of October 31, 2014)
Beijing (110000 BJ)
Tianjin (120000 TJ)
Hebei Province (130000 he)
Shanxi Province (140000 SX)
Inner Mongolia Autonomous Region (150000 NM)
Liaoning Province (210000 LN) br> Jilin Province (220000 JL)
Heilongjiang (230000 HL)
Shanghai (310000 SH)
Jiangsu Province (320000 JS)
Zhejiang (330000 ZJ)
Anhui Province (340000 AH) Fujian (350000 FJ)
Jiangxi Province (360000 JX)
Shandong (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 (500000 CQ)
Sichuan Province (510000 SC)
Guizhou Province (520000 GZ) Yunnan Province (530000 YN)
Tibet Autonomous Region (540000 XZ)
Shaanxi (610000 SN)
Gansu Province (620000 GS)
Qinghai (630000 QH)
Ningxia Hui Autonomous Region (640000 N X)
Xinjiang Uyghur Autonomous Region (650000 XJ)
Taiwan Province (710000 Tw)
Hong Kong Special Administrative Region (810000 HK)
Macao Special Administrative Region (820000 Mo)
The calculation method of the 18th digit (check code) of the ID card
Multiply the 17 digits in front of the ID number by the different coefficients respectively;
The coefficients from first to 17th digits are: 7-9-10-5-8-4-2-1-6-3-7-9-10-5-8-4-2
Adding the results of these 17-digit numbers and coefficients;
Add and divide by 11 to see how much the remainder is;
The remaining number may only have 0-1-2-3-4-5-6-7-8-9-10 these 11 numbers;
The number of the last ID card corresponding to 1-0-x-9-8-7-6-5-4-3-2
It was learned from the above that if the remainder was 2, the ⅹ of Roman numerals would appear on the 18th digit of the ID card. If the remainder is 10, the last number of the ID card is 2.
Example:
A man's ID number is 340523198001010013. We need to see if this ID card is a legal ID.
First we derive the product of the first 17 bits and:
(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
And then ask for more:
185% 11 = 9
Finally, the corresponding rule can know that the number of the remainder 9 corresponds to 3. Therefore, it can be determined that this is a qualified ID number.
JavaScript Verify 18-bit ID
Copy Code code as follows:
var city = {11: "Beijing", 12: "Tianjin", 13: "Hebei", 14: "Shanxi", 15: "Inner Mongolia", 21: "Liaoning", 22: "Jilin", 23: "Heilongjiang", 31: "Shanghai", 32: "Jiangsu", 33: "Zhejiang", 34: "Anhui", 35: "Fujian", 36: "Jiangxi", 37: "Shandong", 41: "Henan", 42: "Hubei", 43: "Hunan", 44: "Guangdong", 45: "Guangxi", 46: "Hainan", 50: "Chongqing", 51: "Sichuan", 52: "Guizhou", 53: "Yunnan", 54: "Tibet", 61: "Shaanxi", 62: "Gansu", 63: "Qinghai", 64: "Ningxia", 65: "Xinjiang", 71: "Taiwan", 81: "Hong Kong", 82: "Macao", 91: "Foreign"};
var ID = ' 340523198001010013 ';
First check the number of digits is 18 digits:
if (!/^\d{17} (\d|x) $/i.test (ID)) return false;
\d Match number
//^ match start
//$ Match end
//I case-insensitive //
{17} match 17 times
//\d|x match number or X
Then verify that the first two digits are valid provinces (municipalities/Autonomous regions):
if (City[id.substr (0,2)] = = = undefined) return to "illegal area";
The Stringobject.substr (start,length) method extracts a specified number of characters from the start subscript in a string
In addition to accessing object properties, you can use a dot (.) Syntax, you can also use brackets ([]) with more flexibility in brackets
Then verify that the date of birth is legal:
var birthday = Id.substr (6, 4) + '/' + number (ID.SUBSTR (2)) + '/' + number (ID.SUBSTR (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 ' illegal birthday ';
The date of the ID card, and then the new date, and then the two dates are the same
//the number () is mainly because ID card dates are 0, and new date () comes out without 0, number () after no 0.
The final check code is correct:
var arrint = [7, 9, 5, 8, 4, 2, 1, 6, 3, 7, 9, ten, 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%];
if (Residue!== id.substr (17, 1)) ' illegal license number ';
If the Colonel has passed the test, it is a legal ID number;
Complete code
<script> function Checkid (ID) {if (typeof ID!== ' string ') return ' illegal string '; var city = {11: "Beijing", 12: "Tianjin", 13: "Hebei", 14: "Shanxi", 15: "Inner Mongolia", 21: "Liaoning", 22: "Jilin", 23: "Heilongjiang", 31: "Shanghai", 32: "Jiangsu", 33: "Zhejiang", 34: "Anhui", 35: "Fujian", 36: "Jiangxi", 37: "Shandong", 41: "Henan", 42: "Hubei", 43: "Hunan", 44: "Guangdong", 45: "Guangxi", 46: "Hainan", 50: "Chongqing", 51: "Sichuan", 52: "Guizhou", 53: "Yunnan", 54: "Tibet",
61: "Shaanxi", 62: "Gansu", 63: "Qinghai", 64: "Ningxia", 65: "Xinjiang", 71: "Taiwan", 81: "Hong Kong", 82: "Macao", 91: "Foreign"};
var birthday = Id.substr (6, 4) + '/' + number (ID.SUBSTR (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 to ' illegal identity card ';
if (City[id.substr (0,2)] = = = undefined) return to "illegal area"; if (Time >= currenttime | | | Birthday!== newbirthday) return ' illegal birthday ';
For (i=0 i<17; i++) {sum = Id.substr (i, 1) * arrint[i];
} residue = arrch[sum% 11];
if (Residue!== id.substr (1)) return to ' illegal ID card oh '; Return City[id.substr (0,2)]+ ", +birthday+", "+ (ID.SUBSTR (16,1)%2?" male ":" Female ")} </script>