Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the commonly used front-end forms to validate JS regular expressions

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces the commonly used front-end form verification JS regular expressions, the article is very detailed, has a certain reference value, interested friends must read it!

In form verification, it is a frequent operation to use regular expressions to verify whether they are correct or not. This paper collates 15 commonly used JavaScript regular expressions, including user name, password strength, integer, number, email address (Email), mobile phone number, ID number, URL address, IPv4 address, hexadecimal color, date, QQ number, WeChat account, license plate number, and Chinese regular.

1 user name regularization

/ / user name is regular, 4 to 16 digits (letters, numbers, underscores, minus signs) var uPattern = / ^ [a-zA-Z0-9 letters -] {4pm 16} $/; / output trueconsole.log (uPattern.test ("caibaojian"))

2 password strength regularity

/ / password strength is regular, at least 6 characters, including at least 1 uppercase letter, 1 lowercase letter, 1 number, 1 special character var pPattern = / ^. * (? =. {6,}) (? =. *\ d) (? =. * [Amurz]) (? =. * [Amurz]) (?) (? =. * [! @ # $% ^ & *?]). / / output trueconsole.log ("=" + pPattern.test ("caibaojian#"))

3 integer regularity

/ / positive integer regular var posPattern = / ^\ dhammer / negative integer regular var negPattern = / ^ -\ dhammer / integer regular var intPattern = / ^ -?\ dhammer / output trueconsole.log (posPattern.test ("42")); / / output trueconsole.log (negPattern.test ("- 42")); / / output trueconsole.log ("- 42"))

4 numeric regularity

It can be an integer or a floating point number.

/ / positive regular var posPattern = / ^\ d *\.?\ negative regular var negPattern = / ^ -\ d *\.??\ number regular var numPattern = / ^ -?\ d *\.?); console.log (negPattern.test ("- 42.2")); console.log (numPattern.test ("- 42.2"))

5 Email regular

/ / Email regular var ePattern = / ^ ([A-Za-z0-9 _\ -\.]) + @ ([A-Za-z0-9 _\ -\.]) +\. ([A-Za-z] {2jin4}) $/; / output trueconsole.log (ePattern.test (99154507@qq.com))

6 regular mobile phone number

/ / regular var mPattern = / ^ 1 [34578]\ d {9} $/; / http://caibaojian.com/regexp-example.html// output trueconsole.log (mPattern.test ("15507621888"))

7 ID card number regular

/ / ID card number (18 digits) 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 trueconsole.log (cP.test ("11010519880605371X"))

8 URL regular

/ / URL regular var urlP= / ^ ((https? | ftp | file):\ /\ /)? ([\ da-z\. -] +)\. ([a murz\.] {2 http://caibaojian.com)); 6}) ([\ /\ w\. -] *) *\ /? $/; / output trueconsole.log (urlP.test (http://caibaojian.com));)

9 IPv4 address regular

/ / ipv4 address regular var ipP = / ^ (?: 25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) {3} (?: 25 [0-5] | 2 [0-4] [0-9] | [01]? [0-9] [0-9]?) $/; / output trueconsole.log (ipP.test ("115.28.47.26"))

10 hexadecimal color regularity

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

11 date regular

/ / regular date, simple judgment, var dP1 = / ^\ d {4} (\ -)\ d {1Magne2}\ 1\ d {1Magne2} $/; / output trueconsole.log (dP1.test ("2017-05-11")); / / output trueconsole.log (dP1.test ("2017-15-11")) / / date regularity Complexity decision var dP2 = / ^ (: (! 0000) [0-9] {4}-(: 0 [1-9] | 1 [0-2])-(: 0 [1-9] | 1 [0-9] | 2 [0-8]) | (?: 0 [13-9] | 1 [0-2])-(?: 29 | 30) | (?: 0 [13578] | 1 [02])-31) | (?: [0-9] {2}) ?: 0 [48] | [2468] [048] | [13579] [26]) | (?: 0 [48] | [2468] [048] | [13579] [26]) 00)-02-29) $/ / / output trueconsole.log (dP2.test ("2017-02-11")); / / output falseconsole.log (dP2.test ("2017-15-11")); / / output falseconsole.log (dP2.test ("2017-02-29"))

12 QQ number regular

/ / QQ sign regular, 5 to 11 digits var qqPattern = / ^ [1-9] [0-9] {4pm 10} $/; / output trueconsole.log (qqPattern.test ("65974040"))

13 Wechat regular number

/ / WeChat account regular, 6 to 20 digits, starting with a letter, letter, number, minus sign, underscore var wxPattern = / ^ [a-zA-Z] ([- _ a-zA-Z0-9] {5pm 19}) + $/; / output trueconsole.log (wxPattern.test ("caibaojian_com"))

14 the license plate number is regular

/ / license plate number regular var cPattern = / ^ [Beijing, Tianjin, Shanghai, Chongqing, Hebei, Henan, Yunnan, Hunan, Anhui, Shandong, new Jiangsu, Zhejiang, Jiangxi, Hubei, Guangxi, Shanxi, Shaanxi, Jilin, Fujian, Gui, Guangdong, Qinghai, Tibet, Sichuan and Ningqiong] {1} [A-Z0-9] {4} [A-Z0-9, Hong Kong and Macao] {1} $/; / output trueconsole.log (cPattern.test ("Guangdong B39006"))

15 contains Chinese rules

/ / contains Chinese regular var cnPattern = / [\ u4E00 -\ u9FA5] /; / / outputs trueconsole.log (cnPattern.test ("Dong Dong")); the above is all the contents of the article "what are the commonly used front-end forms to verify JS regular expressions?" Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report