1 overview

1.1 introduction

At present, 21 commonly used javaScript regular expressions are collected, including user name, password strength, integer, number, Email address (Email), mobile phone number, ID number, URL address, IP address, hexadecimal color, date, wechat signal, license plate number, Chinese regular and so on. Form verification processing essential, hurry to collect it!

Will also join the new regular in succession come in, we give more valuable advice!

2 Regular list

2.1 Regular user names

2.1.1 Regular Basic User names

When doing user registration, the user name regular verification is used.

Define basic user names as follows:

  • Minimum 4 bits, maximum 16 bits {4,16}
  • Can contain lowercase initials [a-z] and uppercase letters [a-z]
  • Can contain numbers [0-9]
  • Can contain the underscore [_] and the minus [-]
  • The first letter must be uppercase
var pattern = / ^ [a zA - Z] [a - zA - Z0 - _ - 9] {3, 15} $/;
/ / output true
console.log("Ifat3."+pattern.test('ifat3'));
/ / output true
console.log("Ifat3."+pattern.test('Ifat3'));
/ / output true
console.log("Ke30."+pattern.test('ke30'));
/ / output is false
console.log("Ke 30."+pattern.test('30ke'));
/ / output is false
console.log("Ke3."+pattern.test('ke3')); The outputfalse
console.log("Ke30 @."+pattern.test('ke30@'));
/ / output is false
console.log("Ke30ke30ke30ke30ke30."+pattern.test('ke30ke30ke30ke30ke30'));
Copy the code

View sample programs

2.1.2 Chinese user names are regular

If Chinese user names are allowed in the rule, the regular expression is changed as follows:

var pattern = / ^ [a zA - Z \ u4E00 - \ u9FA5] [a zA - Z0-9 \ u4E00 - \ u9FA5_ -] {3, 15} $/;
/ / output true
console.log("Ifat3."+pattern.test('ifat3'));
/ / output true
console.log("Ifat3."+pattern.test('Ifat3'));
/ / output true
console.log(Lesson 30 Morrie:+pattern.test('Lesson 30 Morrie'));
/ / output is false
console.log("Ke 30."+pattern.test('30ke'));
/ / output is false
console.log("Ke3."+pattern.test('ke3'));
/ / output is false
console.log("Ke30 @."+pattern.test('ke30@'));
  / / output is false
console.log("Ke30ke30ke30ke30ke30."+pattern.test('ke30ke30ke30ke30ke30'));
Copy the code

[\ u4e00-\ u9FA5] is the regular matching of more than 20,000 basic Chinese characters, in which \u4E00 represents the Chinese character “one”. For details, see The Unicode Encoding Range of Chinese Characters.

View sample programs

2.2 Password strength is regular

// The password strength is regular and contains at least six characters, including at least one uppercase letter, one lowercase letter, one digit, and one special character
var pPattern = / ^. * (? (=. {6})? =.*\d)(? =.*[A-Z])(? =.*[a-z])(? =. * [! @ # $% ^ & *?] ). * $/;
/ / output true
console.log("IFat3 #."+pPattern.test("iFat3#"));
Copy the code

The above regular expressions can only be used to determine the passability of user password strength. For more information about password strength verification, see: Analysis and Implementation of Password Strength Detection Algorithm Based on Rule Scoring.

View sample programs

2.3 Number dependent regularization

2.3.1 Integer regularization

// Positive integers are regular
var posPattern = /^\d+$/;
// Negative integer is regular
var negPattern = /^-\d+$/;
// Integer regular
var intPattern = / ^ -? \d+$/;
/ / output true
console.log("30:"+posPattern.test("30"));
/ / output true
console.log("- 30."+negPattern.test("To 30"));
/ / output true
console.log("- 30."+intPattern.test("To 30"));
Copy the code

View sample programs

2.3.2 Floating point res

// The positive floating-point number is regular
var posPattern = /^\d*\.\d+$/;
// Negative floating point is regular
var negPattern = /^-\d*\.\d+$/;
// Two decimal regulars
var twoPattern = / ^ -? \d*\.\d{2}$/;  
/ / output true
console.log("30.2:"+posPattern.test("30.2"));
/ / output true
console.log("30.2."+negPattern.test("30.2"));
/ / output true
console.log("30.22."+twoPattern.test("30.22"));
Copy the code

View sample programs

2.3.3 Integer floating point regularization

They can be integers or floating-point numbers

// Positive numbers are regular
var posPattern = /^\d*\.? \d+$/;
// Negative numbers are regular
var negPattern = /^-\d*\.? \d+$/;
// Numeric regex
var numPattern = / ^ -? \d*\.? \d+$/;
/ / output true
console.log("30.2:"+posPattern.test("30.2"));
/ / output true
console.log("30.2."+negPattern.test("30.2"));
/ / output true
console.log("30.2."+numPattern.test("30.2"));
Copy the code

View sample programs

2.4 Date Regularization

2.4.1 Date of birth is regular

var pattern = /^((19[2-9]\d{1})|(20((0[0-9])|(1[0-8]))))\-((0? [1-9]) | \ [0-2] (1)) - ((0? [1-9]) | ([1-2] [0-9]) | | 31) $30 /;
/ / output true
console.log(pattern.test("1923-3-18"));
/ / output true
console.log(pattern.test("1923-4-31"));
/ / output true
console.log(pattern.test("1923-2-29"));
/ / output true
console.log(pattern.test("2016-2-29"));
Copy the code

The above regular verification is not perfect, mainly due to the number of days in 2, 4, 6, 9 and 11 months.

View sample programs

2.4.2 General date res

// Date is regular and complex
var dP2 = / ^ (? : (? ! 0000) [0-9] {4} - (? : (? : 0 | [1-9] [0-2] 1) - (? : 0 [1-9] [0-9] | | 1 2 [0 to 8]) | (? : 0 [9] 13 - | [0-2] 1) - (? 30) : 29 | | (? : 0 [13578] 1 [02]) - 31) | | (? : [0-9] {2} (? : 0 [48] | [2468] [048] | [13579] [26]) | (? : 0 [48] | [2468] [048] | [13579] [26]) $/ 00) - 02-29);
/ / output true
console.log(dP2.test("2017-02-11"));
/ / output is false
console.log(dP2.test("2017-15-11"));
/ / output is false
console.log(dP2.test("2017-02-29"));
Copy the code

View sample programs

2.5 Email regular

2.5.1 Basic Email re

var pattern = / ^ ([A Za - z0-9 _ \ - \]) + \ @ ([A - Za - z0-9 _ \ - \]) + \. ([A Za - z] {2, 4}) $/;
/ / output true
console.log(pattern.test('[email protected]'));
/ / output true
console.log(pattern.test('[email protected]'));
/ / output true
console.log(pattern.test('[email protected]'));
/ / output true
console.log(pattern.test('[email protected]'));
/ / output is false
console.log(pattern.test('[email protected]'));
/ / output is false
console.log(pattern.test('Mao Rui @ 30 ke. Cn'));
Copy the code

Basic Email re is the most common authentication method and is suitable for most application scenarios. As you can see from the above tests, this expression does not support domain names ending in. Online and. Store. To accommodate such domain names (more than 4 digits), adjust the limit of the end of the re {2,4} (example: {2,8}). Another problem is that Email usernames cannot include Chinese characters.

View sample programs

2.5.2 Chinese name Email is regular

Based on the problem in the previous re, two additional rules are as follows:

  • The user name can include Chinese characters[\u4e00-\u9fa5]
  • The end of the domain name can be a maximum of 8 digits{8} 2
var pattern = / ^ ([A Za - z0-9 _ \ - \ \ u4e00 - \ u9fa5]) + \ @ ([A - Za - z0-9 _ \ - \]) + \. ([A Za - z] {8} 2) $/;
/ / output true
console.log(pattern.test('[email protected]'));
/ / output true
console.log(pattern.test('[email protected]'));
/ / output true
console.log(pattern.test('[email protected]'));
/ / output true
console.log(pattern.test('[email protected]'));
/ / output true
console.log(pattern.test('[email protected]'));
/ / output true
console.log(pattern.test('Mao Rui @ 30 ke. Cn'));
Copy the code

View sample programs

2.5.3 Email Res for a specific domain name

Before mobile captcha, email authentication was almost the only guarantee of user uniqueness. The emergence of temporary mailboxes (also known as 10-minute mailboxes or disposable mailboxes) makes the mechanism of mailbox authentication and account activation meaningless. Temporary email addresses are not enumerable, so we can only whitelist them and only allow limited email domain names to pass verification.

var pattern = /^([A-Za-z0-9_\-\.])+\@(163.com|qq.com|30ke.cn)$/;
/ / output true
console.log(pattern.test('[email protected]'));
/ / output is false
console.log(pattern.test('[email protected]'));
/ / output true
console.log(pattern.test('[email protected]'));
/ / output true
console.log(pattern.test('[email protected]'));
/ / output is false
console.log(pattern.test('[email protected]'));
/ / output is false
console.log(pattern.test('Mao Rui @ 30 ke. Cn'));
Copy the code

This method guarantees validation security, but if the whitelist is too long, the pattern string will be too long. In this case, you can write the email domain name whitelist as an array, use regular expression to do the preliminary verification, and use the whitelist to do the secondary verification.

Common domain name whitelist array:

var domains= ["qq.com"."163.com"."vip.163.com"."263.net"."yeah.net"."sohu.com"."sina.cn"."sina.com"."eyou.com"."gmail.com"."hotmail.com"];
Copy the code

The above whitelist only lists 11 common email domain names. You can add or delete them as needed.

View sample programs

2.6 Mobile Phone Number is regular

// The phone number is regular
var mPattern = / ^ ([0-9] (13) | | (14 [5] | 7) (15 ([0, 3] | [5-9])) | (18 [0, 5-9])) \ d {8} $/;
/ / output true
console.log(mPattern.test("18600000000"));
Copy the code

View sample programs

2.7 Id number regularization

// Id number (18 digits) is regular
var cP = /^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
/ / output true
console.log(cP.test("11010519880605371X"));
Copy the code

The above regularization can only be used to determine the basic passability of the ID number. For more information about the determination of the citizenship number, please refer to the document: Correctness determination of the Citizenship Number and Program Implementation

View sample programs

2.8 the URL regular

/ / URL regular
var urlP= /^((https? |ftp|file):\/\/)? ([\ \. Da - z -] +) \. ([a-z \.] {2, 6}) (/ \ \ / \ w - *) * \ /? $/;
/ / output true
console.log(urlP.test("http://30ke.cn"));
Copy the code

View sample programs

2.9 the IP address

2.9.1 IPv4 Address Is Regular

// The ipv4 address is regular
var ipP = / ^ (? : (? : 25 [0 to 5] | 2 [0 to 4] [0-9] | [01]? [0-9] [0-9]?) \.) {3} (? : 25 [0 to 5] | 2 [0 to 4] [0-9] | [01]? [0-9] [0-9]?) $/;
/ / output true
console.log(ipP.test("115.28.47.26"));
Copy the code

View sample programs

2.9.2 IPv6 Addresses are regular

/ / IPV6 is regular
var pattern = / (([0-9 a - fA - F] {1, 4} {7, 7}) [0-9 a - fA - F] {1, 4} | ([0-9 a - fA - F] {1, 4} {1, 7}) : | ([0-9 a - fA - F] {1, 4} {1, 6}) : [0-9 a - fA - F] {1, 4} | ([0-9 a - fA - F] {1, 4} {1, 5}) (: [0-9 a - fA - F] {1, 4}, {1, 2} | ([0-9 a - fA - F] {1, 4} {1, 4}) (: [0-9 a - fA - F] {1, 4}, {1, 3} | ([0-9 a - fA - F] {1, 4} {1, 3}) (: [0-9 a - fA - F] {1, 4} {1, 4} | ([0-9] a - fA - F {1, 4}} {1, 2) (: [0-9 a - fA - F] {1, 4}, {1, 5} | [0-9 a - fA - F] {1, 4} : ((: [0-9 a - fA - F] {1, 4}, {1, 6}) | : ((: [0-9 a - fA - F] {1, 4} {1, 7} | :) | fe80: : [0-9 a - fA - F] {0, 4}) {0, 4} % [0-9 a zA - Z] {1} | : : (FFFF (: 0 {1, 4}) {0, 1} {0, 1} ((25) [0 to 5) | (2 [0 to 4] | 1 {0, 1} [ 0-9] [0-9] {0, 1}) \.) {3} (25 [0 to 5) | (2 [0 to 4] | 1 {0, 1} [0-9]) {0, 1} [0-9]) | ([0-9 a - fA - F] {1, 4} {1, 4}) : ((25 [0 to 5) | (2 [0 to 4] | 1 {0, 1} [0-9]) {0, 1} [0-9]) \.) {3} (25 [0 to 5) | (2 | 1 [0-4] [0-9] {0, 1}) [0-9] {0, 1})) /;
/ / output true
console.log(pattern.test("fe80:0000:0000:0000:0204:61ff:fe9d:f156"));
Copy the code

View sample programs

2.10 Hexadecimal color is regular

//RGB Hex color is regular
var cPattern = / ^ #? ([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;
/ / output true
console.log(cPattern.test("#b8b8b8"));
Copy the code

View sample programs

2.11 QQ Number is regular

// The QQ id is regular, 5 to 11 digits
var qqPattern = / ^ (1-9] [0-9] {4, 10} $/;
/ / output true
console.log(qqPattern.test("65974040"));
Copy the code

View sample programs

2.12 Wechat signal is regular

// Micro signal regular, 6 to 20 characters, starting with a letter, letter, digit, minus sign, underscore
var wxPattern = / ^ [a zA - Z] ([- _a - zA - Z0-9] {5} 3) + $/;
/ / output true
console.log(wxPattern.test("RuilongMao"));
Copy the code

View sample programs

2.13 The license plate is regular

// The license plate is regular
var cPattern = / ^ [beijing-tianjin Shanghai yu ji yu cloud liao black xiang anhui new GuiGan of hubei province can Sue your yue jin meng shan ji min qinghai-tibet plain NingQiong that brought A - Z] {1} {1} [a-z] [A - Z0-9] {4} [A - Z0-9 hang cop Hong Kong and Macao] {1} $/;
/ / output true
console.log(cPattern.test("Beijing K39006"));
Copy the code

View sample programs

2.14 Contains Chinese regulars

// Contains Chinese regulars
var cnPattern = /[\u4E00-\u9FA5]/;
/ / output true
console.log(cnPattern.test("30 classes"));
Copy the code

View sample programs

3 the end

3.1 conclusion

My level is limited, if there are mistakes, please do not hesitate to correct! thank you