JavaScript implements id card verification code

  • 2020-12-20 03:29:50
  • OfStack

The meaning of 18 id card numbers

1 or 2 province, autonomous region and municipality codes;
3-4 prefecture-level city, league and autonomous prefecture codes;
5-6 county, county-level city and district codes;
7 to 14. For example, 19670401 represents April 1, 1967.
The numbers 15-17 are serial numbers, in which 17 are singular males and even females;
The 18 bits are check codes, 0-9 and X, which are randomly generated by the formula.

For example:

The meaning of the ID number:
34 Anhui Province
Ma 'anshan city
23 for the county
Date of Birth (1 January 1980)
001 is the serial number (1 is singular, representing male)
3 is the verification code

Administrative division code

Latest County and above 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)
Jilin Province (220000 JL)
Heilongjiang Province (230,000 HL)
Shanghai (310000 SH)
Jiangsu Province (320000 JS)
Zhejiang Province (330000 ZJ)
Anhui Province (340,000 AH)
Fujian Province (350000 FJ)
Jiangxi Province (360,000 JX)
Shandong Province (370,000 SD)
Henan Province (410000 HA)
Hubei Province (420000 HB)
Hunan Province (430000 HN)
Guangdong Province (440,000 GD)
Guangxi Zhuang Autonomous Region (450000 GX)
Hainan Province (460000 HI)
Chongqing municipality (500000 CQ)
4 Chuan Province (510000 SC)
Guizhou Province (520000 GZ)
Yunnan Province (530000 YN)
Tibet Autonomous Region (540,000 XZ)
Shaanxi Province (610,000 SN)
Gansu Province (620000 GS)
Qinghai Province (630,000 QH)
Ningxia Hui Autonomous Region (640,000 NX)
Xinjiang Uygur Autonomous Region (650000 XJ)
Taiwan Province (710000 Tw)
Hong Kong SAR (810,000 HK)
Macao Special Administrative Region (820000 Mo)

Id number 18 (check code) calculation method
Multiply the 17 digits in front of the ID card number by different coefficients;
From 1 to 107 coefficient respectively: 7-9-10-5-8-4-5-8-4-3-7-9-7-9-10-4-2
Add up the 17 digits and the coefficients;
You add it up and you divide by 11, and you see what the remainder is;
The remainder can only be 11 numbers, 0-1-2-4-5-6-7-8-9-10;
The corresponding last digit id card number is 1-0-ES103en-9-8-7-6-5-4-3-2
Through to know that if the remainder is 2 above in id appears on the 18th of digital Ⅹ Roman numerals. If the remainder is 10, the last digit of the ID card is 2.
For example:
The ID number of a man is 340523198001010013. We need to see if this is a legitimate ID card.

First, we get the product 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
And then the remainder:
185 % 11 = 9
And then we know by the corresponding rule that the remainder of 9 is 3. Therefore, it can be determined that this is a qualified ID number.

JavaScript verify 18-digit ID card

var city = {11:" Beijing ",12:" tianjin ",13:" hebei ",14:" shanxi ",15:" Inner Mongolia ",21:" liaoning ",22:" Ji Lin ",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:"4 sichuan ",52:" guizhou ",53:" yunnan ",54:" Tibet  ",61:" shaanxi ",62:" gansu ",63:" qinghai ",64:" ningxia ",65:" xinjiang ",71:" Taiwan ",81:" Hong Kong ",82:" Macau ",91:" foreign "};
var ID = '340523198001010013';

First, check whether the number of digits is 18:


if(!/^\d{17}(\d|x)$/i.test(ID)) return false;
// \d   Match the Numbers 
// ^   Match began 
// $   Match the ending 
// i   Case insensitive 
// {17}  matching 17 time 
// \d|x  Match number or x

Then check if the first two are legitimate provinces (municipalities/autonomous regions) :


if(city[ID.substr(0,2)] === undefined) return " Illegal region ";

The stringObject. substr(start,length) method extracts a specified number of characters from the start subscript in a string
// In addition to the dot (.) syntax, you can also use brackets ([]) to access object properties, making them more flexible

Then check if the date of birth is valid:


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 ' Illegal birthday ';
//  Obtain id card of year month day, and then again  new 1 a  Date And compare the two dates 1 to 
//  Here with Number() Mainly because the date of the ID card is taken 0 , and new Date() The date comes out without it 0 The, Number() After that, nothing 0 the 

Finally, judge 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)) ' Illegal number ';

If the above verification are passed, it is a legal ID number;

The complete code


<script>
 function checkID(ID) {
  if(typeof ID !== 'string') return ' Invalid string ';
  var city = {11:" Beijing ",12:" tianjin ",13:" hebei ",14:" shanxi ",15:" Inner Mongolia ",21:" liaoning ",22:" Ji Lin ",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:"4 sichuan ",52:" guizhou ",53:" yunnan ",54:" Tibet  ",61:" shaanxi ",62:" gansu ",63:" qinghai ",64:" ningxia ",65:" xinjiang ",71:" Taiwan ",81:" Hong Kong ",82:" Macau ",91:" foreign "};
  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 ' Illegal identity card ';
  if(city[ID.substr(0,2)] === undefined) return " Illegal region ";
  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(17, 1)) return ' Illegal ID card ';
 
  return city[ID.substr(0,2)]+","+birthday+","+(ID.substr(16,1)%2?"  male ":" female ")
 }
</script>


Related articles: