Regular expression and verification of ID number (JavaScript, Regex)

Briefly

When doing user real-name verification, regular expressions and verification schemes for ID numbers are often used. This article lists two verification schemes. You can choose the suitable scheme according to the actual situation of your project.

idcard-check

ID number description

Resident ID number, the correct and official title should be "citizen ID number". According to the regulations on citizen ID number in [National Standard of the People's Republic of China GB 11643-1999], the citizen ID number is a feature combination code, which consists of a seventeen-digit body code and a one-digit check code. The order from left to right is: six-digit address code, eight-digit birth date code, three-digit sequence code and one-digit check code.

Take a female ID card number in Chaoyang District, Beijing as an example, the meaning of the ID card number is shown in the following figure:

Analysis of the meaning of ID number

Note: The ID number comes from the national standard [GB 11643-1999].

Next, we will complete a complete ID number verification process from scratch.

Option 1 (simple)

1.1 Division Rules

We first propose scheme 1, and define the following rules step by step:

1.1.1 Address code rules:

  • The address code is 6 digits long
  • starts with numbers 1-9
  • The last 5 digits are numbers 0-9

According to the above rules, write the regular expression of the address code: /^[1-9]\d{5}/

1.1.2 Year code rules:

  • Year code is 4 digits long
  • starts with the numbers 18, 19 or 20
  • The remaining two digits are 0-9

According to the above rules, write the regular expression of the year code:  /(18|19|20)\d{2}/. If you don't need a year starting with 18, you can drop the 18.

1.1.3 Month code rules:

  • Month code is 2 digits long
  • The first digit is 0, the second digit is 1-9
  • Or the first digit is 1 and the second digit is 0-2

According to the above rules, write the regular expression of the month code:  /((0[1-9])|(1[0-2]))/.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325255460&siteId=291194637