JavaScript Regular expression verifies ID number is legal (two methods) _ Regular expression

Source: Internet
Author: User
Tags getdate

The first method:

In the user registration page Some requirements are more stringent, need to verify the identity card JS is legitimate, through this function strictly this system software, thus filtering to many water passengers. Here's how to achieve this method for you to explain.

Most of the time we are using a set of regular expressions to determine whether the user entered the identity card is legitimate, that in the regular expression of judgment before the composition of the ID number you know how much? Here's how much information is included in an ID number:

1, the structure of the number

The citizenship number is a characteristic combination code, which consists 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 (top six digits)

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 (seventh to 14 digits)

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. Sequence code (15th to 17 digits)

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 (18th digits)

As a tail check code, is by the number compilation unit according to unify formula calculates, if someone's tail is 0-9, does not appear x, but if tail is 10, then must use the X to replace, because if uses 10 to do tail, then this person's identity card becomes 19 places. X is 10 of the Roman numeral, substituting X for 10, which guarantees that the citizen's identity card conforms to the national standard.

After knowing the meaning of the structure of the ID card, we begin to enter the topic:

1. Define the object of a national area

var acity={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, the regular expression of judgment

function Iscardid (sId) {
 var isum=0;
 var info= "";
 if (!/^\d{17} (\d|x) $/i.test (sId)) return "The ID card length or format error you entered";
 Sid=sid.replace (/x$/i, "a");
 if (Acity[parseint (Sid.substr (0,2))]==null) return "illegal in your identity card area";
 Sbirthday=sid.substr (6,4) + "-" +number (Sid.substr (10,2)) + "-" +number (Sid.substr (12,2));
 var d=new Date (Sbirthday.replace (/-/g, "/"));
 if (sbirthday!= (d.getfullyear () + "-" + (D.getmonth () +1) + "-" + d.getdate ()) return "The date of birth on the ID card is illegal";
 for (var i = 17;i>=0;i-) Isum + = (Math.pow (2,i)%) * parseint (Sid.charat (17-i), one);
 if (isum%11!=1) return "The ID number you entered is illegal";
 Acity[parseint (Sid.substr (0,2))]+ ", +sbirthday+", "+ (SID.SUBSTR (16,1)%2?" Male ":" female ")//This can also determine the entry of the ID number of the person sex return
 true;
}

The second method:

function Sccard () {var Sctype=document.getelementbyid ("Sc_card_type"). Value; 
      if (sctype== "1") {var Sccard=document.getelementbyid ("Sc_card_num"). Value; 
      if (sccard.length!=0) {if (!checkcard (Sccard)) {$ ("#errorTips"). HTML ("ID card number format error"); 
      }else{$ ("#errorTips"). HTML (""); 
 }} return false; //function Checkidno (obj) {var vcity={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"}; 
  Checkcard = function (obj) {//var card = document.getElementById (' card_no '). Value; 
  Is null//if (Card = = = ")//{//return false; 
  //Checksum length, type if (Iscardno (obj) = False) {return false; //Check the province if (checkprovince (obj) = false) {REturn false; 
  //Verify Birthday if (checkbirthday (obj) = False) {return false; 
  //Check bit detection if (checkparity (obj) = False) {return false; 
 return true; 
 }; Check whether the number conforms to the specification, including length, type Iscardno = function (obj) {//ID number is 15 or 18 digits, 15 digits are all digits, 18 digits before 17 digits, and last is parity digit, possibly digit or character x var reg =/(^\d{15}$) | (^\d{17} (\d| 
  X) $)/; 
  if (reg.test (obj) = False) {return false; 
 return true; 
 }; 
  Check the first two digits of the ID card, verify the Province checkprovince = function (obj) {var province = obj.substr (0,2); 
  if (vcity[province] = = undefined) {return false; 
 return true; 
 }; 
  Check that birthdays are correct checkbirthday = function (obj) {var len = obj.length; ID 15, the order for the province (3) city (3-bit) year (2-bit) month (2-bit) (2-bit) (3-digit) check-digit, all are digital if (len = = ') {var re_fifteen =/^ (\d{6}) (\d{2}) (\d{2}) (\d {2}) 
   (\d{3}) $/; 
   var arr_data = Obj.match (Re_fifteen); 
   var year = arr_data[2]; 
   var month = arr_data[3]; 
   var day = arr_data[4]; 
   var birthday = new Date (' +year+ '/' +month+ '/' +day); REturn verifybirthday (' +year,month,day,birthday '); //ID 18, in the Order of the province (3-bit) City (3-bit) (4-bit) (2-bit) (2-bit) (4-digit) check digit end may be x if (len = = '% ') {var re_eighteen =/^ (\d{6}) (\d{4} ) (\d{2}) (\d{2}) (\d{3}) ([0-9]| 
   X) $/; 
   var arr_data = Obj.match (Re_eighteen); 
   var year = arr_data[2]; 
   var month = arr_data[3]; 
   var day = arr_data[4]; 
   var birthday = new Date (year+ '/' +month+ '/' +day); 
  Return Verifybirthday (Year,month,day,birthday); 
 return false; 
 }; 
  Checksum date Verifybirthday = function (year,month,day,birthday) {var now = new Date (); 
  var now_year = Now.getfullyear ();  Is it reasonable if (birthday.getfullyear () = Year && (birthday.getmonth () + 1) = = Month && birthday.getdate () 
   = = Day) {//judge the range of year (between 3 and 100 years old) var time = now_year-year; 
   if (time >= 0 && time <= 130) {return true; 
  return false; 
 return false; 
 }; Check bit detection checkparity = function (obj) {//15-bit to 18-bit obj = ChangefivteentoeIghteen (obj); 
  var len = obj.length; 
   if (len = = ') {var arrint = new Array (7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); 
   var arrch = new Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 '); 
   var cardtemp = 0, I, valnum; 
   for (i = 0; i < i + +) {cardtemp + = Obj.substr (i, 1) * arrint[i]; 
   } valnum = arrch[cardtemp% 11]; 
   if (Valnum = = Obj.substr (1)) {return true; 
  return false; 
 return false; 
 }; 15-bit 18-bit ID card number Changefivteentoeighteen = function (obj) {if (obj.length = = ') {var arrint = new Array (7, 9 
   , 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2); 
   var arrch = new Array (' 1 ', ' 0 ', ' X ', ' 9 ', ' 8 ', ' 7 ', ' 6 ', ' 5 ', ' 4 ', ' 3 ', ' 2 ');  
   var cardtemp = 0, I; 
   obj = obj.substr (0, 6) + ' n ' + obj.substr (6, obj.length-6); 
   for (i = 0; i < i + +) {cardtemp + = Obj.substr (i, 1) * arrint[i]; 
   obj + + arrch[cardtemp% 11]; 
  return obj; } return Obj };

Related Article

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.