JS verify real name and ID number match _javascript skill

Source: Internet
Author: User

Recent projects used in the need to call the real-name authentication interface, real-name authentication interface price compared to SMS is not a bit higher than a few cents, so that the conditions of the call authentication is strict, so use to JS verify the true name and JS authentication ID number.

Get to the point.

JS Verify the real name, is used to match the Unicode characters, and the Chinese name is generally 2-4, so repeat the match {2,4} times

1.js Verify real name

 var regname =/^[\ue-\ufa]{,}$/;
 if (!regname.test (name)) {
  alert (' True name filled in error ');
  return false;
 }

JS Verification ID number, China's identity card number, a generation of ID number is 15 digits, the second generation ID cards are 18 digits, the last one in addition to a check digit may be ' x ' or ' x ', so there are four possibilities: a.15 digit b.18 digit c.17 digit number, 18th digit is ' x ' d.17 Digit, 18th digit is ' x '

2.js Authentication ID Number

 var Regidno =/(^\d{}$) | (^\d{}$) | (^\d{} (\d| X|X) $)/;
 if (!regidno.test (Idno)) {
  alert (' identity card number filled in error ');
  return false;
 }

Ps:15 bit and 18-bit ID JS verification instance

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, Central Municipality code table/Provinceandcitys: {: "Beijing", "Tianjin",: "Hebei",: "Shanxi",: "Inner Mongolia", "Liaoning",: "Jilin", "Heilongjiang",: "Shanghai", "Jiangsu",: "Zhejiang",: "Anhui",: "Fujian",: "Jiangxi",: "Shandong",: "Henan", "Hubei", ":" Hunan ",:" Guangdong ",:" Guangxi "," Hainan ",:" Chongqing "," Sichuan ",:" Guizhou "," Yunnan ",:" Tibet ",:" Shaanxi "," Gansu ",": " Qinghai ",:" Ningxia ",:" Xinjiang "," Taiwan "," Hong Kong "," Macao ",:" Overseas "},/* each weighting factor * * Powers: [" "," "," "," "," "," "," "," "," "", "", "", "", "", "", "", "", "" " ,/* Paritybit: ["", "", "X", "", "", "", "", "", "", "" "" "," "," "", "* * * gender * * Genders: {male:" male ", Female:" Female "},/* Verify address code * * Checkad
  Dresscode:function (addresscode) {var check =/^[-]\d{}$/.test (Addresscode);
  if (!check) return false;
  if (Idcardnoutil.provinceandcitys[parseint (addresscode.substring (,))] {return true;
  }else{return false; },/* Verify date code */checkbirthdaycode:function (birdaycode) {var check =/^[-]\d{} ([-]) | ( [-])) (([-])| ([-][-])| ([-]))
  $/.test (Birdaycode); 
  if (!check) return false;
  var yyyy = parseint (birdaycode.substring (,),);
  var mm = parseint (Birdaycode.substring (,),);
var dd = parseint (birdaycode.substring (),); var xdata = new Date (YYYY,MM-,DD); if (XData > New Date ()) {return false;//birthday cannot be greater than the current date}else if ((xdata.getfullyear () = = yyyy) && (Xdata.getm
 Onth () = = mm-) && (xdata.getdate () = = dd) {return true;
 }else{return false; 
  /* Compute the School Check code */getparitybit:function (IDCARDNO) {var id = idcardno.substring (,);
  /* Weighted * var power =;
  for (Var i=;i<;i++) {Power + = parseint (Id.charat (i),) * parseint (idcardnoutil.powers[i));
  /* * modulo/var mod = power%;
 return Idcardnoutil.paritybit[mod];
  },/* Verify School Check code/checkparitybit:function (Idcardno) {var paritybit = Idcardno.charat (). toUpperCase ();
  if (idcardnoutil.getparitybit (idcardno) = = Paritybit) {return true;
  }else{return false; },/* check digit or bit ID number/checkidcardno:function (IDCARDNO) {//bit and bit ID number of basic check var check =/^\d{}| ( \d{} (\d|x|
 X)) $/.test (Idcardno);
 if (!check) return false;
 The judgment length is bit or bit if (idcardno.length==) {return Idcardnoutil.checkidcardno (Idcardno); }else if (idcardno.length==) {return Idcardnoutil.checkidcardno (Idcardno);
 }else{return false; },//check digit ID number checkidcardno:function (idcardno) {//bit ID number basic check var check =/^[-]\d{} (([-]) | ( [-])) (([-])| ([-][-])| ([-])) 
  \d{}$/.test (Idcardno);
  if (!check) return false;
  Verify address code var Addresscode = idcardno.substring (,);
  Check = Idcardnoutil.checkaddresscode (Addresscode);
  if (!check) return false;
  var birdaycode = ' + idcardno.substring (,);
 Check Date code return Idcardnoutil.checkbirthdaycode (Birdaycode); },//check digit ID number checkidcardno:function (idcardno) {//bit ID number basic format check var check =/^[-]\d{}[-]\d{} ([[]) | ( [-])) (([-])| ([-][-])| ([-])) \d{} (\d|x|
  X) $/.test (Idcardno);
  if (!check) return false;
  Verify address code var Addresscode = idcardno.substring (,);
  Check = Idcardnoutil.checkaddresscode (Addresscode);
  if (!check) return false;
  Check Date code var Birdaycode = idcardno.substring (,);
  Check = Idcardnoutil.checkbirthdaycode (Birdaycode);
  if (!check) return false; Verify School Check code return IDCARDNOUtil.checkparitybit (Idcardno);
  }, Formatedatecn:function (day) {var yyyy =day.substring (,);
  var mm = day.substring (,);
  var dd = day.substring ();
 return yyyy + '-' + mm + '-' + dd;
  },//Get information Getidcardinfo:function (Idcardno) {var idcardinfo = {gender: "",//Sex Birthday: ""//Birth date (YYYY-MM-DD)
 };
  if (idcardno.length==) {var aday = ' + idcardno.substring (,);
  IDCARDINFO.BIRTHDAY=IDCARDNOUTIL.FORMATEDATECN (Aday);
  if (parseint (Idcardno.charat ())%==) {Idcardinfo.gender=idcardnoutil.genders.female;
  }else{Idcardinfo.gender=idcardnoutil.genders.male;
  }}else if (idcardno.length==) {var aday = idcardno.substring (,);
   IDCARDINFO.BIRTHDAY=IDCARDNOUTIL.FORMATEDATECN (Aday);
  if (parseint (Idcardno.charat ())%==) {Idcardinfo.gender=idcardnoutil.genders.female;
  }else{Idcardinfo.gender=idcardnoutil.genders.male;
 } return idcardinfo;
 /* bit-turn/getid:function (Idcardno) {if (idcardno.length==) {return idcardno;
  }else if (idcardno.length==) {Return idcardno.substring (,) + idcardno.substring (,);
 }else{return null; },/* bit-position/getid:function (Idcardno) {if (idcardno.length==) {var id = idcardno.substring (,) + ' + idcardno.substr
  ING ();
  var paritybit = idcardnoutil.getparitybit (ID);
 return ID + paritybit;
 }else if (idcardno.length==) {return idcardno;
 }else{return null;
}
 }
}; ID Number Verification JQuery.validator.addMethod ("Idcardno", function (value, Element) {return this.optional (element) | | idcardnou 
Til.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 if the same identity card already exists in the database

(1) If the input is 15 ID card: First find a 15-bit ID exists, if there is no need to 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.

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.