Summary of the most commonly used regular validation rules for web pages

As a web front-end development, there are many regular validation rules commonly used. Here are some common examples:

1. Verify mobile number

The regular expression of the mobile phone number can be adjusted according to the format of the mobile phone number in different countries and regions. The following is a regular expression for mobile phone numbers in mainland China:

const regex = /^1[3456789]\d{9}$/;

 

In this example, the regular expression ^1[3456789]\d{9}$means :

  • ^Indicates the beginning of the matched string.
  • 1Indicates must start with the number 1.
  • [3456789]Indicates that the second digit must be one of 3, 4, 5, 6, 7, 8 or 9.
  • \d{9}Indicates that 9 digits must follow.
  • $Indicates the end of the matched string.

2. Verify email address

The regular expression of the email address can be adjusted according to different email service providers. Here is a general regular expression for email addresses:

const regex = /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/;

In this example, the regular expression ^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$means :

  • ^Indicates the beginning of the matched string.
  • \w+Indicates must begin with one or more letters, numbers, or underscores.
  • ([-+.]\w+)*Indicates can consist of one or more hyphens, plus signs, or periods, followed by one or more letters, numbers, or underscores.
  • @Indicates that an @ symbol must be included.
  • \w+Indicates must contain one or more letters, numbers, or underscores.
  • ([-.]\w+)*Indicates can consist of one or more hyphens or periods, followed by one or more letters, numbers, or underscores.
  • \.Indicates that a period must be included.
  • \w+Indicates must contain one or more letters, numbers, or underscores.

3. Verify ID number

The regular expression of ID number can be adjusted according to different countries and regions. The following is the regular expression for the ID card number in mainland China:

const regex =
  /^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}[0-9Xx]$/;

// 身份证号码为15位或18位,15位时全为数字,18位前17位为数字,最后一位是校验位,可能为数字或字符X
// const regex = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;

In this example, the regular expression ^[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[012])(0[1-9]|[12]\d|3[01])\d{3}[0-9Xx]$means :

  • ^Indicates the beginning of the matched string.
  • [1-9]Indicates that the first digit must be one of 1 to 9.
  • \d{5}Indicates that 5 digits must follow.
  • (19|20)Indicates that the seventh to tenth digits must be 19 or 20.
  • \d{2}Indicates that the eleventh to twelfth digits must be a two-digit number.
  • (0[1-9]|1[012])Indicates that the 13th to 14th digits must be one of 01 to 12.
  • (0[1-9]|[12]\d|3[01])Indicates that the 15th to 16th digits must be one of 01 to 31.
  • \d{3}Indicates that the seventeenth to nineteenth digits must be three numbers.
  • [0-9Xx]Indicates that the last digit can be a number or a capital letter X.

4. Verify password strength

To verify the strength of a password, factors such as the length of the password, character type, and combination method need to be considered. The following is a simple regular expression for password strength, requiring passwords to be between 6 and 20 characters long and must contain letters and numbers:

const regex = /^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]{6,20})$/;

In this example, the regular expression ^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]{6,20})$means :

  • ^Indicates the beginning of the matched string.
  • (?=.*[0-9])Indicates that it must contain at least one number.
  • (?=.*[a-zA-Z])Representation must contain at least one letter.
  • ([a-zA-Z0-9]{6,20})Indicates that the password must be 6 to 20 characters long and contain only letters and numbers.
  • $Indicates the end of the matched string.

5. Verify the URL address

Validating a URL address usually takes into account the protocol, hostname, port, and path parts of the URL. The following is a simple URL validation regular expression:

const regex = /^(http|https):\/\/([\w-]+\.)+[\w-]+(:\d+)?(\/\S*)?$/;

// const regex =
//   /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;

In this example, the regular expression ^(http|https):\/\/([\w-]+\.)+[\w-]+(:\d+)?(\/\S*)?$means :

  • ^Indicates the beginning of the matched string.
  • (http|https)Indicates that it must start with http or https.
  • :\/\/means match a colon and a double slash.
  • ([\w-]+\.)+[\w-]+Indicates to match a hostname, which must contain at least one hyphen or letter followed by one or more letters, numbers, or hyphens.
  • (:\d+)?Indicates to match an optional port number and must end with a colon and one or more digits.
  • (\/\S*)?Indicates to match an optional path, which must start with a slash and can be followed by any non-whitespace characters.
  • $Indicates the end of the matched string.

Of course, there are some other commonly used regular check rules, such as:

6. Verify zip code

The regular expression of zip code can be adjusted according to different countries and regions. The following is a regular expression for postal codes in mainland China:

const regex = /^[1-9]\d{5}$/;

In this example, the regular expression ^[1-9]\d{5}$means :

  • ^Indicates the beginning of the matched string.
  • [1-9]Indicates that the first digit must be one of 1 to 9.
  • \d{5}Indicates that 5 digits must follow.
  • $Indicates the end of the matched string.

7. Verify IP address

The regular expressions for IP addresses can be adjusted for different IP address formats. The following is a simple IPv4 address validation regular expression:

const regex =
  /^([01]?\d{1,2}|2[0-4]\d|25[0-5])\.([01]?\d{1,2}|2[0-4]\d|25[0-5])\.([01]?\d{1,2}|2[0-4]\d|25[0-5])\.([01]?\d{1,2}|2[0-4]\d|25[0-5])$/;

In this example, the regular expression ^([01]?\d{1,2}|2[0-4]\d|25[0-5])\.([01]?\d{1,2}|2[0-4]\d|25[0-5])\.([01]?\d{1,2}|2[0-4]\d|25[0-5])\.([01]?\d{1,2}|2[0-4]\d|25[0-5])$means :

  • ^Indicates the beginning of the matched string.
  • ([01]?\d{1,2}|2[0-4]\d|25[0-5])Indicates to match a number between 0 and 255.
  • \. display number.
  • $Indicates the end of the matched string.

8. Verify passport number

The following is a regular expression for mainland China passport numbers:

const regex = /^[EeGg]\d{8}$/;

In this example, the regular expression ^[EeGg]\d{8}$means :

  • ^Indicates the beginning of the matched string.
  • [EeGg]Indicates that the first digit must be either an uppercase letter E or G, or a lowercase letter e or g.
  • \d{8}Indicates that 8 digits must follow.
  • $Indicates the end of the matched string.

9. Verify the number of the Hong Kong and Macau Exit Permit

The following is the regular expression for the Hong Kong and Macau Exit Permit Number of Mainland China:

const regex = /^[HMhm]\d{8}$/;

In this example, the regular expression ^[HMhm]\d{8}$means :

  • ^Indicates the beginning of the matched string.
  • [HMhm]Indicates that the first digit must be either an uppercase letter H or M, or a lowercase letter h or m.
  • \d{8}Indicates that 8 digits must follow.
  • $Indicates the end of the matched string.

10. Verify Chinese name

The regular expression of the Chinese name can be adjusted according to the specific situation. The following is a simple regular expression for checking Chinese names, requiring Chinese names to contain only Chinese characters:

const regex = /^[\u4e00-\u9fa5]{2,4}$/;

In this example, the regular expression ^[\u4e00-\u9fa5]{2,4}$means :

  • ^Indicates the beginning of the matched string.
  • [\u4e00-\u9fa5]Indicates to match any Chinese character.
  • {2,4}Indicates to match 2 to 4 Chinese characters.
  • $Indicates the end of the matched string.

In addition, if you want to check whether the Chinese name contains characters other than Chinese characters, you can use a regular expression similar to the following:

const regex = /^[\u4e00-\u9fa5·]{2,20}$/;

In this example, the regular expression ^[\u4e00-\u9fa5·]{2,20}$means :

  • ^Indicates the beginning of the matched string.
  • [\u4e00-\u9fa5·]Indicates to match any Chinese character or Chinese spacer "·".
  • {2,20}It means to match 2 to 20 Chinese characters or the Chinese spacer "·".
  • $Indicates the end of the matched string.

In this example, "·" is used as a spacer for Chinese names. This is a common way, but it is not the only correct way. In practical applications, the appropriate spacer should be selected according to the specific situation, and adjusted and optimized as needed.

11. Verify the foreigner's residence permit number

The following is a simple regular expression for validating a foreigner's residence permit, which requires that the residence permit number only contain English letters and numbers:

const regex = /^[A-Za-z0-9]{8,20}$/;

In this example, the regular expression ^[A-Za-z0-9]{8,20}$means :

  • ^Indicates the beginning of the matched string.
  • [A-Za-z0-9]Indicates to match any English letter or number.
  • {8,20}Indicates to match 8 to 20 English letters or numbers.
  • $Indicates the end of the matched string.

12. Validation only allows letters and numbers

The regular expression to allow only letters and numbers is as follows:

const regex = /^[A-Za-z0-9]+$/;

In this example, the regular expression ^[A-Za-z0-9]+$means :

  • ^Indicates the beginning of the matched string.
  • [A-Za-z0-9]Indicates to match any English letter or number.
  • +means match the preceding character at least one or more times.
  • $Indicates the end of the matched string.

This regular expression can be used to check strings containing only letters and numbers, such as usernames, passwords, etc. Note that this regular expression does not include spaces and other special characters. If you need to check a string including spaces and other special characters, you can adjust and optimize it according to the specific situation.

13. Verify that the first digit is not zero

The regular expression for numbers with a non-zero first digit is as follows:

const regex = /^[1-9]\d*$/;

In this example, the regular expression ^[1-9]\d*$means :

  • ^Indicates the beginning of the matched string.
  • [1-9]Indicates that the first digit must be one of 1 to 9.
  • \d*Indicates that any number of digits can follow (including zero digits).
  • $Indicates the end of the matched string.

This regular expression can be used to check integers. It is required that the first digit of the integer must be one of 1 to 9, and it can contain multiple numbers. In addition, this regular expression does not include decimals and negative numbers. If you need to check numbers that include decimals and negative numbers, you can adjust and optimize according to the specific situation.

It should be noted that the above example is only one of the commonly used regularization verification rules, which may need to be adjusted and optimized according to specific business requirements in actual applications. At the same time, the writing of regular expressions also needs to take into account factors such as performance and readability.

Guess you like

Origin blog.csdn.net/lwf3115841/article/details/130927596