Note 1. The following verification rules are summarized in the company project. You need to modify the rules if some rules are not suitable for you. 2. These rules are written by myself and my colleagues before, and may not be the latest. Welcome to submit them.

Commonly used class

The user name

// User name is regular, with 4 to 16 characters (letters, digits, underscores, minus signs)
var uPattern = / ^ [a - zA - Z0 - _ - 9] $/ dec {4};
// Print true for example: xiaohua_qq
console.log(uPattern.test("caibaojian"));
Copy the code

Password strength

// 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])(? =. * [! @ # $% ^ & *?] ). * $/;
// Print true, for example, Kd@curry666
console.log(pPattern.test("Kd@curry666"));

// Weak password (less than 6 characters in length, including letters, digits, and special characters)
var pPattern1 = / (? . = {6}). * $/;
/ / output true
console.log(pPattern1.test('asdf'))

// Medium strength password (the password must be 7 or more characters, including two letters and digits, and special characters are optional)
var pPattern2 = / ^ (? = {7}) (((? =.*[A-Z])(? =.*[a-z]))|((? =.*[A-Z])(? =. * [0-9]) | ((? =.*[a-z])(? =. * [0-9]))). * $/;
/ / output true
console.log(pPattern2.test('adb123adg'))

// Strong password (longer than 8 characters, must contain uppercase letters, lowercase letters, digits, special characters) (not verified yet)
var pPattern3 = / ^ (? 1 = {8}) (? =.*[A-Z])(? =.*[a-z])(? =. * ([0-9])? =.*\\W).*$/;
/ / output true
console.log(pPattern3.test('Aa123##asd'))
Copy the code

The pure digital

// Pure digital regular
var posPattern = /^\d{1,}$/;
// Print true, for example, 12345678
console.log(posPattern.test("42"));
Copy the code

Consists of numbers and letters

// Whether to use numbers and letters to form the re
var posPattern = /^[A-Za-z0-9]+$/;
// Print true for example: james666, haha233hi
console.log(posPattern.test("james666"));
Copy the code

Plain English letter

// Pure English letters are regular
var posPattern = /^[a-zA-Z]+$/;
// Print true for example: Russel
console.log(posPattern.test("Russel"));
Copy the code

The value consists of lowercase letters

// Pure lowercase letters form regex
var posPattern = /^[a-z]+$/;
// Print true for example: russel
console.log(posPattern.test("russel"));
Copy the code

Pure uppercase English letters

// Pure uppercase English letters are regular
var posPattern = /^[A-Z]+$/;
// Print true for example: ABC, KD
console.log(posPattern.test("ABC"));
Copy the code

Contains only Chinese characters and numbers

// Contains only Chinese and numeric regulars
var numP = / ^ ((? :[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\u D86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD 86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])|(\d))+$/;
// Print true for example: Hahaha, hello 6
console.log(numP.test("Ha ha ha."));
Copy the code

Cannot contain letters

// Cannot contain letters
var numP = /^[^A-Za-z]*$/;
// output true for example: hello 6, @¥()!
console.log(numP.test("@ RMB ()!"));
Copy the code

Specifies whether the account is valid. It must start with a letter and contain 5 to 16 characters. Alphanumeric and underscore (_) are allowed

// Whether the account is valid (alphanumeric, alphanumeric, and underscore)
var numP = / ^ [a zA - Z] [a - zA - Z0-9 _] {4, 15} $/;
For example: Justin, justin1989,justin_666
console.log(numP.test("justin_666"));
Copy the code

Pure Chinese/Chinese characters

// Pure Chinese/Chinese regular
var numP = / ^ (? :[\u3400-\u4DB5\u4E00-\u9FEA\uFA0E\uFA0F\uFA11\uFA13\uFA14\uFA1F\uFA21\uFA23\uFA24\uFA27-\uFA29]|[\uD840-\uD868\uD86A-\u D86C\uD86F-\uD872\uD874-\uD879][\uDC00-\uDFFF]|\uD869[\uDC00-\uDED6\uDF00-\uDFFF]|\uD86D[\uDC00-\uDF34\uDF40-\uDFFF]|\uD 86E[\uDC00-\uDC1D\uDC20-\uDFFF]|\uD873[\uDC00-\uDEA1\uDEB0-\uDFFF]|\uD87A[\uDC00-\uDFE0])+$/;
// Print true for example: re, front end
console.log(numP.test("Regular"));
Copy the code

If the decimal

// Whether the decimal is regular
var numP = /^\d+\.\d+$/;
// Print true for example: 0.0, 0.09
console.log(numP.test("0.09"));
Copy the code

Integer regular

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

Number of regular

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+$/;
console.log(posPattern.test("42.2"));
console.log(negPattern.test("42.2"));
console.log(numPattern.test("42.2"));
Copy the code

Email

Regular / / Email
var ePattern = /^[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@[a-zA-Z0-9](? : [a - zA - Z0-9 -], 21 {0} [a zA - Z0-9])? (? :\.[a-zA-Z0-9](? : [a - zA - Z0-9 -], 21 {0} [a zA - Z0-9])?) * $/;
// Print true for example: [email protected], [email protected]
console.log(ePattern.test("[email protected]"));
Copy the code

Domestic Mobile Number

// China mobile number (strict), according to the latest mobile number segment released by the Ministry of Industry and Information Technology in 2019
var numP = / ^ (? : (? : \ | + 00) 86)? 1 (? : (? :3[\d])|(? : 4 [5-7 | 9]) | (? : 5 [0-3 | 5-9]) | (? : 6 [5-7]) | (? : 7-8 0) | (? :8[\d])|(? :9[1|8|9]))\d{8}$/;
// Output true for example, 008618311006933, +8617888829981, 19119255642
console.log(numP.test("19119255642"));


// Chinese mobile phone number (loose), as long as it starts with 13,14,15,16,17,18,19
var mPattern = / ^ (? : (? : \ | + 00) 86)? 1[3-9]\d{9}$/;
// Output true for example, 008618311006933, +8617888829981, 19119255642
console.log(mPattern.test("19119255642"));


// If your mobile number is used to receive SMS messages, it is recommended to choose this one
var numP1 = / ^ (? : (? : \ | + 00) 86)? 1\d{10}$/;
// Output true for example, 008618311006933, +8617888829981, 19119255642
console.log(numP1.test("19119255642"));


//http://caibaojian.com/regexp-example.html
Copy the code
  • Rule description:

    The domestic mobile phone number has 11 digits, and the first two digits can only be: 13\14\15\17\18.

  • Number segment allocation of the three carriers

Them roughly An optional value
China Mobile
13x 134X(0-8), 135, 136, 137, 138, 139
14x 147(Data Card)
15x X(0-7\9)(TD), 158, 159
17x 187 (3G \4G), 188(3G)
18x Extension numbers are optional
19x 98(Please see Appendix I for clarification)
China Unicom
13x 130, 131, 132
14x 145(Data Card)
15x 155, 156,
17x 176 (4 g)
18x 185 (3 g), 186 (3 g)
16x 166(Please see Appendix I for clarification)
China Telecom
13x 133, (1349 WEitong)
15x 153
17x 177 (4G), 173 (recently opened)
18x 180(3G), 181(3G), 189(3G)
19x 199(2017-08-16 see Appendix I for clarification)
Virtual operator
170

Domestic landline number

// Line number regular (can not need area code)
var mPhone = / ^ (0 \ d {2, 3})? (-)? \ d {7, 8} (\ d {1, 6})? $/;
// Output true, for example, 23091234
console.log(mPhone.test("23091234"));

// Domestic landline number, such as 0341-86091234
var mPhone = /\d{3}-\d{8}|\d{4}-\d{7}/;
// Output true for example, 0936-4211235
console.log(mPhone.test("0936-4211235"));
Copy the code
  • Rule description:

    TIP format: area code-number-extension number

type instructions note
The area code The area code starts with 0 and has 3 or 4 digits
number A number consists of seven or eight digits
immediately The extension number consists of 1-6 digits
The area code and the number can be connected without a hyphen or with a hyphen
Extension numbers are optional

First Generation ID Number (15 digits)

// Generation ID number (15 digits) is regular
var numP = /^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$/;
// Output true, for example, 622001790131123
console.log(numP.test("622001790131123"));
Copy the code

The second generation ID number (18 digits), ending with a check bit, may be a number or character X

// Second generation ID number (18 digits), the last digit is the check bit, may be a number or character X regular
var nump = /^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$/;
For example, 62222319991205131x
console.log(nump.test("62222319991205131x"));
Copy the code

Id number, supporting 1/2 generation (15 /18 digits)

// Id id number, supporting 1/2 generation (15 /18 digit) regular
var nump = /(^\d{8}(0\d|10|11|12)([0-2]\d|30|31)\d{3}$)|(^\d{6}(18|19|20)\d{2}(0\d|10|11|12)([0-2]\d|30|31)\d{3}(\d|X|x)$)/;
// Print true for example, 622223199912051311
console.log(nump.test("622223199912051311"));
Copy the code

The resources

The first-generation ID card will not be used by the National Id Card Number Inquiry Service Center since January 1, 2013

The date of

var a = /^((((1[6-9]|[2-9]\d)\d{2})-(0? [02] [13578] | 1) - (0? [1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0? [012] [13456789] | 1) - (0? [1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0? 2 - (0? [1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0? $/ 2-29));

// select * from time to time
var dP1 = / ^ \ d {4}) (\ \ d {1, 2} \ \ d {1, 2} $1 /;
/ / output true
console.log(dP1.test("2017-05-11"));
/ / output true
console.log(dP1.test("2017-15-11"));


// 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

ip-v4

// ipv4 address regular ipv4 :(1 to 255). (0 to 255). (0 to 255). (0 to 255)
var ipP = /^(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)\.( 1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|\d)$/;
// Print true for example: 172.16.0.0, 127.0.0.0
console.log(ipP.test("115.28.47.26"));
Copy the code

ip-v6

// The ipv6 address is regular
var ipP = / ^ ((([0-9] a - Fa - f {1, 4} :) {7} [0-9 a - Fa - f] {1, 4}) | (([0-9 a - Fa - f] {1, 4} {6}) : [0-9 a - Fa - f] {1, 4}) | (([0-9 a - Fa - f] {1, 4} {5}) : ([0-9 - Fa - a F] {1, 4} :)? [0-9 a - Fa - f] {1, 4}) | (([0-9 a - Fa - f] {1, 4} {4}) : ([0-9 a - Fa - f] {1, 4}} {0, 2) [0-9 a - Fa - f] {1, 4}) | (([0-9 a - Fa - f] {1, 4} {3}) : ([0-9 a - Fa - f] {1, 4} {0, 3)} [0-9] a - Fa - f {1, 4}) | (([0-9 a - Fa - f] {1, 4} {2}) : ([0-9 a - Fa - f] {1, 4} {0, 4}) [0-9 a - Fa - f] {1, 4}) | (([0-9 a - Fa - f] {1, 4} {6}) ( (\ b ((25 [0 to 5]) | | \ d {2} (1) (2 \ [0-4] d) | (\ d {1, 2})) \ b) \.) {3} (\ b ((25 [0 to 5]) | | \ d {2} (1) (2 \ [0-4] d) | (\ d {1, 2})) \ b)) | (([0-9 a - Fa - f] {1, 4} {0, 5}) : ((\ b ((25 [0 to 5]) | | \ d {2} (1) (2 \ [0-4] d) | (\ d {1, 2 }))\b)\.) {3} (\ b ((25 [0 to 5]) | | \ d {2} (1) (2 \ [0-4] d) | (\ d {1, 2})) \ b)) | (: : ([0-9 a - Fa - f] {1, 4} {0, 5}) ((\ b ((25 [0 to 5]) | | \ d {2} (1) (2 \ [0-4] d) | (\ d {1, 2}))\b)\.) {3} (\ b ((25 [0 to 5]) | | \ d {2} (1) (2 \ [0-4] d) | (\ d {1, 2})) \ b)) | ([0-9 a - Fa - f] {1, 4} : : ([0-9 a - Fa - f] {1, 4} {0, 5}) [0-9 a - Fa - f] {1, 4}) | (: : ([0 9 a - Fa - f] {1, 4} {0, 6}) [0-9 a - Fa - f] {1, 4}) | (([0-9 a - Fa - f] {1, 4} {1, 7}) :)) $/ I;
/ / output true such as: 2031, 0000:130 f, 0000:0000-09 c0:876 a: 130 b
console.log(ipP.test("2031:0000:130f:0000:0000:09c0:876a:130b"));
Copy the code

Hexadecimal color

// The hexadecimal color is regular
var colorP = / ^ #? ([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/;
// Print true for example: #f00, #F90, #00, #fe9de8
console.log(colorP.test("#fe9de8"));
Copy the code

China Postcode

// Chinese zip code is regular
var colorP = /^(0[1-7]|1[0-356]|2[0-7]|3[0-6]|4[0-7]|5[1-7]|6[1-7]|7[0-5]|8[013-6])\d{4}$/;
// Print true for example, 734500, 100101
console.log(colorP.test("734500"));
Copy the code

QQ number

// the QQ number is regular
var qqP = / ^ (1-9] [0-9] {4, 10} $/;
// Print true, for example, 903013545, 9020304
console.log(qqP.test("12345"));
Copy the code

WeChat ID

//6 to 20 characters, starting with a letter, a letter, a digit, a minus sign, and an underscore
var weCP = / ^ [a zA - Z] [- _a - zA - Z0-9] {5} 3 $/;
// Print true, for example, github666, kd_-666
console.log(weCP.test("wexin123_-"));
Copy the code

Certificate Number

License plate number

/** * Regular license plate number (it is allowed to start with Chinese characters only, followed by six characters, consisting of uppercase English letters and Arabic numerals. For example: Yue B12345) * The last character of the license plate (the last character is the Chinese character, the Chinese characters include hang, learn, police, Hong Kong, ao. For example: Guangdong Z1234 Port) * New Army license plate (starting with two capital English letters, followed by five Arabic digits. For example: BA12345. * Green license plate (special license plate for new energy vehicles) (The new license plate type in 2017 is only allowed to start with Chinese characters, followed by seven characters, which are composed of uppercase English letters and Arabic numerals. For example: Guangdong B123456) */
// The license plate is regular
var reg = / ^ [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} $/;
var reg2 =/ ^ [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] {6} $/;
/ / output true
console.log(reg.test("Guangdong B39006"));
Copy the code

Hong Kong and Macao Pass

// Hong Kong and Macao Pass is regular
var reg = /^[CW]\d{8}$/;
/ / output true
console.log(reg.test("C00000000"));
Copy the code
type instructions note
It starts in capital EnglishCorW C stands for electronic, or e-card. W is for book
This is followed by eight digits

The resources

People’s Republic of China travel Permit for Hong Kong and Macao

link

Thunderbolt link

// Thunderlink re
var linkP = /^thunder:\/\/[a-zA-Z0-9]+=$/;
/ / output is true, for example: thunder://QUEsICdtYWduZXQ6P3h0PXVybjpidGloOjBCQTE0RTUxRkUwNjU1RjE0Qzc4NjE4RjY4NDY0QjZFNTEyNjcyOUMnWlo=
console.log(linkP.test("thunder://QUEsICdtYWduZXQ6P3h0PXVybjpidGloOjBCQTE0RTUxRkUwNjU1RjE0Qzc4NjE4RjY4NDY0QjZFNTEyNjcyOUMnWlo="))
Copy the code

Ed2k links (loose matching)

// ED2K link (loose match) re
var linkP = /^ed2k:\/\/|file|.+|\/$/;
// Print true for example: ed2k://|file|%E5%AF%84%E7%94%9F%E8%99%AB.PARASITE.2019.HD-1080p.X264.AAC-UUMp4(ED2000.COM).mp4|2501554832|C0B93E0879C607 1CBED732C20CE577A3|h=5HTKZPQFYRKORN52I3M7GQ4QQCIHFIBV|/
console.log(linkP.test("ed2k://|file|%E5%AF%84%E7%94%9F%E8%99%AB.PARASITE.2019.HD-1080p.X264.AAC-UUMp4(ED2000.COM).mp4|2501554832|C0B93E0879C60 71CBED732C20CE577A3|h=5HTKZPQFYRKORN52I3M7GQ4QQCIHFIBV|/"))
Copy the code

Magnetic linking (loose matching)

// magnetic link (loose match) re
var linkP = /^magnet:\? xt=urn:btih:[0-9a-fA-F]{40,}.*$/;
// Prints true for example: magnet:? xt=urn:btih:40A89A6F4FB1498A98087109D012A9A851FBE0FC
console.log(linkP.test("magnet:? xt=urn:btih:40A89A6F4FB1498A98087109D012A9A851FBE0FC"))
Copy the code

Subnet mask

// The subnet mask is regular
var linkP = / ^ (? D: \ \ d {1, 2} 1 \ | d | 2 \ [0-4] d | 25 ([0 to 5])? : \. (? : 1 \ \ d {1, 2} | d \ | 2 d \ [0-4] d | 25 [0-5])) {3} $/;
// Output true for example, 255.255.255.0, 255.224.0.0
console.log(linkP.test("255.255.255.0"))
Copy the code

This article will continue to collect regular validation in front-end forms…