How to use JavaScript regular expressions to verify that ID numbers are legitimate--regular expressions

Source: Internet
Author: User

In the user registration page Some requirements are more stringent, the need to verify the identity card 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, regular expression 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 above content analysis is the entire content of this article, the realization method is very simple, mainly understands the individual ID number number represents the meaning, very easy!

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.