15-bit and 18-bit ID card JS Check simple example _javascript skills

Source: Internet
Author: User

I. Structure and representation of the ID card number

1, the structure of the number

According to the national standard Gb11643-1999〗 of the People's Republic of China, the citizenship number is a feature combination code, consisting of a 17 digit ontology code and a check code. The order is from left to right: six digit address code, eight digit birth date code, three digit sequential code and one digit parity code.

2. Address Code

The administrative division code of the county (city, Flag, district) of the resident account of the encoding object is executed according to the GB/T2260 regulations.

3. Birth date Code

Indicates the year, month, and day of the encoding object's birth, which is executed according to gb/t7408, and the year, month, and day codes are not separated by delimiters.

4. Sequential code

Indicates the sequence number assigned to a person born in the same same address code, the same month and the same day, and the odd distribution of order codes to men, even to females.

5. Check code

(1) The weighted summation formula of 17-digit body code

S = SUM (Ai * Wi), i = 0, ..., 16, first the sum of the first 17 digits
Ai: Indicates the ID number value of position I
Wi: A weighted factor representing position I
7 9 10 5 8 4 2 1 6 3 7 9 10 5 8 4-2

(2) Computational model

Y = mod (S, 11)

(3) The corresponding check code is obtained through the model

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
That is, if you get the remainder of 1 then the final check bit p should be the corresponding 0.

15-digit number:
A A b b c c y y m m D x x S
18-digit number:
A A b b c c y y y m m D x x s P

Second, address code

ID Card 6 for the administrative division of the number of digital code (referred to as "GB/T 2260-2007 Code of Administrative division of the People's Republic of China"): The number of coding principles and structure analysis, it uses three-level six-bit hierarchical code structure, according to the level of our respective provinces Special Administrative Region), the city (region, Autonomous Prefecture, the Union), the county (autonomous counties, counties, flags, autonomous flags, municipal districts, forest areas, SAR).

(1) 1th, 2 digits: The province, the provinces, autonomous regions, municipalities, special administrative regions.

Province, central Municipality code table: {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 "}

(2) 3rd, 4 digits: The provincial city (county) code, said the city, region, Autonomous Prefecture, AU, municipalities under the jurisdiction of the District, county summary Code, Province (autonomous region) of the county-level administrative division of the total code.

--01~20, 51~70 said that the city, the central municipality 01 said the urban area, 02 after the municipality under the jurisdiction of the suburban counties;

--21~50 said that the region, the autonomous Prefecture, the Union;

--90 said the province (autonomous region) territory-level Administrative division summary Code.

(3) 5th, 6 digits: The location of the county (city) code, said county, autonomous counties, counties, flags, autonomous flags, municipal districts, forest areas, special zone.
--01~20 said that the municipal district, the region (Autonomous Prefecture, the union) governs the county, the city jurisdiction and the province (autonomous region) territory-level administrative division, 01 usually expresses the jurisdiction summary code;

--21~80 said counties, autonomous counties, flags, autonomous flags, forest areas, regional jurisdiction;

--81~99 says the province (autonomous region) governs the county.

Third, other

15-digit ID number:

The 7th and 8 digits are the year of birth (two digits, expressed in the latter two digits of the year),
9th, 10 is the birth month,
11th, 12 representatives of the date of birth,
13th, 14 bits are sequential codes, this is a random number,
The 15th digit is representative, odd is male, even number is female.

18-digit ID number:

7th, 8, 9 and 10 are the year of birth (four digits),
11th, 12 is the birth month,
13th, 14 representatives of the date of birth,
The 15th and 16 digits are sequential codes,
The 17th digit is representative, odd is male, even number is female.
The last one is a check digit.

Four, JS code example

var idcardnoutil = {* * province, the Central Municipality code table * * Provinceandcitys: {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 "},/* each weighting factor/powers: [" 7 "," 9 "," 10 "," 5 "," 8 "," "4", "2", "1", "6", "3", "7", "9", "10", "5", "8", "4", "2"],/* 18th School Check code */PARITYB it: ["1", "0", "X", "" 9 "," 8 "," 7 "," 6 "," 5 "," 4 "," 3 "," 2 "], * * gender/genders: {male:" male ", Female:" Female "},/* Verify Address code * * * Checkaddresscode
   : function (addresscode) {var check =/^[1-9]\d{5}$/.test (Addresscode);
   if (!check) return false;
   if (Idcardnoutil.provinceandcitys[parseint (addresscode.substring (0,2))] {return true;
   }else{return false; },/* Verify date code */checkbirthdaycode:function (birdaycode) {var check =/^[1-9]\d{3} ((0[1-9)) | ( 1[0-2])) ((0[1-9]) | ( [1-2] [0-9]) | (3[0-1])
   $/.test (Birdaycode);  if (!check) return false;
   var yyyy = parseint (birdaycode.substring (0,4), 10);
   var mm = parseint (birdaycode.substring (4,6), 10);
  var dd = parseint (birdaycode.substring (6), 10);
  var xdata = new Date (YYYY,MM-1,DD); if (XData > New Date ()) {return false;//birthday cannot be greater than the current date}else if ((xdata.getfullyear () = = yyyy) && (xdata.ge
  Tmonth () = = mm-1) && (xdata.getdate () = = dd)) {return true;
  }else{return false;  
   /* Compute the School Check Code/getparitybit:function (Idcardno) {var id17 = idcardno.substring (0,17);
   /* Weighted * var power = 0;
   for (Var i=0;i<17;i++) {Power + = parseint (Id17.charat (i), ten) * parseint (idcardnoutil.powers[i));
   /* * modulo/var mod = power% 11;
 return Idcardnoutil.paritybit[mod];
   /* Verify the School Check Code/checkparitybit:function (Idcardno) {var paritybit = Idcardno.charat. toUpperCase ();
   if (idcardnoutil.getparitybit (idcardno) = = Paritybit) {return true;
   }else{return false; },/* Verify 15-bit or 18-digit ID number/CHECKIDCARDNO: function (Idcardno) {//15-bit and 18-bit ID number base check var check =/^\d{15}| ( \D{17} (\d|x|
  X)) $/.test (Idcardno);
  if (!check) return false;
  The judgment length is 15-bit or 18-bit if (idcardno.length==15) {return Idcardnoutil.check15idcardno (Idcardno);
  }else if (idcardno.length==18) {return Idcardnoutil.check18idcardno (Idcardno);
  }else{return false; },//Verify 15-bit ID number check15idcardno:function (idcardno) {//15-bit ID number basic check var check =/^[1-9]\d{7} ((0[1-9)) | ( 1[0-2])) ((0[1-9]) | ( [1-2] [0-9]) | (3[0-1])  
   \d{3}$/.test (Idcardno);
   if (!check) return false;
   Verify address code var Addresscode = idcardno.substring (0,6);
   Check = Idcardnoutil.checkaddresscode (Addresscode);
   if (!check) return false;
   var birdaycode = ' n ' + idcardno.substring (6,12);
 Check Date code return Idcardnoutil.checkbirthdaycode (Birdaycode); },//verify 18-bit ID number check18idcardno:function (idcardno) {//18-bit ID number basic format check var check =/^[1-9]\d{5}[1-9]\d{3} (0[1-9 ])| (1[0-2]) ((0[1-9]) | ([1-2][0-9]) | (3[0-1]) \D{3} (\d|x| X) $/.test (idcardNo);
   if (!check) return false;
   Verify address code var Addresscode = idcardno.substring (0,6);
   Check = Idcardnoutil.checkaddresscode (Addresscode);
   if (!check) return false;
   Check Date code var birdaycode = idcardno.substring (6,14);
   Check = Idcardnoutil.checkbirthdaycode (Birdaycode);
   if (!check) return false;  
 Verify the school Check code return idcardnoutil.checkparitybit (IDCARDNO);
    }, Formatedatecn:function (day) {var yyyy =day.substring (0,4);
    var mm = day.substring (4,6);
    var dd = day.substring (6);
 return yyyy + '-' + mm + '-' + dd; },//Get information Getidcardinfo:function (Idcardno) {var idcardinfo = {gender: "",//Gender Birthday: ""//Birth date (yyyy-mm
  -DD)};
    if (idcardno.length==15) {var aday = ' n ' + idcardno.substring (6,12);
    IDCARDINFO.BIRTHDAY=IDCARDNOUTIL.FORMATEDATECN (Aday);
    if (parseint (Idcardno.charat)%2==0) {Idcardinfo.gender=idcardnoutil.genders.female;
    }else{Idcardinfo.gender=idcardnoutil.genders.male; }}else if (Idcardno.length==18) {var aday = idcardno.substring (6,14);
     IDCARDINFO.BIRTHDAY=IDCARDNOUTIL.FORMATEDATECN (Aday);
    if (parseint (Idcardno.charat)%2==0) {Idcardinfo.gender=idcardnoutil.genders.female;
    }else{Idcardinfo.gender=idcardnoutil.genders.male;
 } return idcardinfo;
  /*18 bit to 15-bit * * Getid15:function (Idcardno) {if (idcardno.length==15) {return idcardno; 
  }else if (idcardno.length==18) {return idcardno.substring (0,6) + idcardno.substring (8,17);
  }else{return null; },/*15 18-bit * * Getid18:function (Idcardno) {if (idcardno.length==15) {var id17 = idcardno.substring (0,6) + ' 19
    ' + idcardno.substring (6);
    var paritybit = idcardnoutil.getparitybit (ID17);
  return id17 + paritybit;
  }else if (idcardno.length==18) {return idcardno;
  }else{return null;

}
 }
}; ID Number Verification JQuery.validator.addMethod ("Idcardno", function (value, Element) {return this.optional (element) | | idcardno Util.checkidcardno (value); 

"Please specify a valid ID number."); 
Get ID information var idcardinfo = Idcardnoutil.getidcardinfo (Idcardno); 

Alert (Idcardinfo.gender + "|" + Idcardinfo.birthday); 
Note: Enter and determine whether the database has the same identity card (1) If the input is a 15-bit ID card: First find 15-bit ID exists, if there is no need to be 15-bit ID card converted to 18 ID card, still do not exist can input the system. 
(2) If the input is 18 ID card: First find a 18-bit ID exists, if there is no need to 18-bit ID card converted to 15 ID card, still do not exist can input the system.
 If you find the corresponding 15-bit ID, you need to update the 15-bit to 18-bit.

Above this 15-bit and 18-bit ID card JS Check simple example is a small series to share all the content, I hope to give you a reference, but also hope that we support the cloud-dwelling community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.