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

JavaScript regular expression validation code

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains the "JavaScript regular expression verification code", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's train of thought slowly in depth, together to study and learn "JavaScript regular expression verification code"!

RegExp: is short for regular expression (regular expression).

A regular expression describes the pattern object of a character. You can use regular expressions to describe what you want to retrieve.

A simple pattern can be a single character. More complex patterns include more characters and can be used for parsing, format checking, substitution, and so on.

/ / determine whether the input is empty function IsNull () {var str = document.getElementById ('str'). Value.trim (); if (str.length==0) {alert (' Sorry, the text box cannot be empty or blank!') / / Please change the "text box" to the name of the attribute you need to verify!}} / / determine whether the date type is in YYYY-MM-DD format function IsDate () {var str = document.getElementById ('str'). Value.trim (); if (str.dater) {var reg = / ^ (\ d {1pm 4}) (- |\ /) (\ d {1pm 2}))\ 2 (\ d {1pm 2}) $/; var r = str.match (reg) If (r==null) alert ('sorry, the date you entered is not in the correct format!'); / / Please change the "date" to the name of the attribute you need to verify!} / / determine whether the date type is the YYYY-MM-DD hh:mm:ss format type function IsDateTime () {var str = document.getElementById ('str'). Value.trim () If (str.recording 2}) {var reg = / ^ (\ d {1meme4}) (- |\ /) (\ d {1meme2})\ 2 (\ d {1Power2})) (\ d {1Magne2}): (\ d {1mem2}) $/; var r = str.match (reg); if (r==null) alert ('Sorry, the date you entered is not in the correct format!') / / Please change the "date" to the attribute name you need to verify!}} / / determine whether the date type is the hh:mm:ss format type function IsTime () {var str = document.getElementById ('str'). Value.trim () If (str.dater str 0) {reg=/ ^ ((20 | 21 | 22 | 23 | [0-1]\ d)\: [0-5] [0-9]) (\: [0-5] [0-9])? $/ if (! reg.test (str)) {alert ("Sorry, the date you entered is not in the correct format!") / / Please change the "date" to the name of the attribute you need to verify!} / / determine whether the characters entered are the letters function IsLetter () {var str = document.getElementById ('str'). Value.trim (); if (str.datedate 0) {reg=/ ^ [a-zA-Z] + $/; if (! reg.test (str)) {alert ("Sorry, the letter type you entered is not in the correct format!") / / Please change the "alphabet type" to the name of the attribute you need to verify!} / / determine whether the input character is an integer function IsInteger () {var str = document.getElementById ('str'). Value.trim (); if (str.alphanumeric type 0) {reg=/ ^ [- +]?\ dalphabet; if (! reg.test (str)) {alert ("Sorry, the integer type you entered is not in the correct format!") / / Please replace the "integer type" with the name of the attribute you want to verify! } / / determine whether the character entered is a double-precision function IsDouble (val) {var str = document.getElementById ('str'). Value.trim (); if (str.roomthread0) {reg=/ ^ [-\ +]?\ d + (\.\ d +)? $/; if (! reg.test (str)) {alert ("Sorry, the double type you entered is not in the correct format!") / / Please replace the "double precision type" with the name of the attribute you want to verify! } / / determine whether the input character is: a str' ZJ 0-9 function IsString () {var str = document.getElementById ('str'). Value.trim (); if (str.string characters) {reg=/ ^ [a-zA-Z0-9 _] + $/; if (! reg.test (str)) {alert ("Sorry, the string type you entered is not in the correct format!") / / Please replace the "string type" with the name of the attribute you want to verify! } / / determine whether the input character is Chinese function IsChinese () {var str = document.getElementById ('str'). Value.trim (); if (str.string _ types0) {reg=/ ^ [\ u0391 -\ uFFE5] + $/; if (! reg.test (str)) {alert ("Sorry, the string type you entered is not in the correct format!"); / / Please replace the "string type" with the attribute name you want to verify! } / / determine whether the input EMAIL format is correct: function IsEmail () {var str = document.getElementById ('str'). Value.trim (); if (str.roomthread0) {reg=/ ^\ w + ([- +.]\ w +) * @\ w + ([-.]\ w +) *.\ w + ([-.]\ w +) * $/; if (! reg.test (str)) {alert ("Sorry, the string you entered is not in the correct format!") / / Please replace the "string type" with the name of the attribute you want to verify! } / / determine whether the zip code entered (only six digits) is correct function IsZIP () {var str = document.getElementById ('str'). Value.trim (); if (str.string _ types0) {reg=/ ^\ d {6} $/; if (! reg.test (str)) {alert ("Sorry, the string type you entered is not in the correct format!"); / / Please replace the "string type" with the attribute name you want to verify! } / / determine that the input number is less than a specific number function MaxValue () {var val = document.getElementById ('str'). Value.trim (); if (str.roomthread0) {reg=/ ^ [- +]?\ dnumbers If (! reg.test (str)) {/ / determine whether it is the number type if (val > parseInt ('123')) / / the maximum value set for yourself {alert (' sorry, the number you entered is out of range'); / / please change the "number" to the attribute name you want to verify! }}

Phone: / ^ (\ (\ d {2pr 3}\)) | (\ d {3}\ -))? (\ (0\ d {2jue 3}\) | 0\ d {2jue 3} -)? [1-9]\ d {6jue 7} (\ -\ d {1jue 4})? $/

Mobile: / ^ (\ (\ d {2jue 3}\)) | (\ d {3}\ -))? 13\ d {9} $/

Url: / ^ http:\ /\ / [A-Za-z0-9] +\. [A-Za-z0-9] + [\ / =\?%\-& _ ~ `@ [\]: +!] * ([^\ "\"]) * $/

IdCard: / ^\ d {15} (\ d {2} [A-Za-z0-9])? $/

QQ: / ^ [1-9]\ d {4jin8} $/

Some kind of special amount: / ^ ((\ d {1jue 3} (,\ d {3}) *) | (\ d +)) (\.\ d {2})? $/ / description: except for "XXX XX,XXX XX,XXX.00" format

/ / provide the .trim () attribute String.prototype.trim=function () {return this.replace (/ (^\ s*)) / g, "");} for each JS authentication method provided above.

Call:

/ / write the JS verification function var patterms = new Object () that you want to call in onClick; / / verify that IPpatterms.ip = / ^ (\ d {1 ~ 2} | 1\ d\ d | 2 [0-4]\ d | 25 [0-5]) (\. (\ d {1 ~ 2} | 1\ d\ d | 2 [0-4]\ d | 25 [0-5])) {3} $/) / / verify EMAILpatterms.email = / ^ [a-zA-Z0-9 dates -] + @ [a-zA-Z0-9 dates -] + (\ .[ a-zA-Z0-9 cycles -] +) + $/; / / verify date format 2009-07-13patterms.date = / ^\ d {4}-(0? [1-9] | 1 [0-2])-(0? [1-9])-(0? [1-9] | [1-2]\ d | 3 [0-1]) $/ / / Verification time format 16:55:39patterms.time = new RegExp ("^ ([0-1]\\ d | 2 [0-3]): [0-5]\\ d: [0-5]\\ d"); / / Verification function function verify (str,pat) {var thePat;thePat = patterms [pat]; if (thePat.test (str)) {return true;} else {return false }} / / Test alert (verify ("asidycom@163.com", "email") + "," + verify ("192.168.1.1", "ip") + "," + verify ("16:55:39", "time") + "," + verify ("2009-07-13", "date") + "," + verify ("192.168", "ip"))

Verification number: ^ [0-9] * $

Verify the n-digit number: ^\ d {n} $

Verify at least n digits: ^\ d {n,} $

Verify the number of mmurn digits: ^\ d {mcentine n} $

Verify zero and non-zero numbers: ^ (0 | [1-9] [0-9] *) $

Verify the positive real number with two decimal places: ^ [0-9] + (. [0-9] {2})? $

Verify a positive real number with 1-3 decimal places: ^ [0-9] + (. [0-9] {1Jue 3})? $

Verify non-zero positive integers: ^\ +? [1-9] [0-9] * $

Verify non-zero negative integers: ^\-[1-9] [0-9] * $

Verify that non-negative integers (positive integers + 0) ^\ dflowers $

Verify non-positive integer (negative integer + 0) ^ ((-\ d +) | (0 +)) $

Verify a character of length 3: ^. {3} $

Verify the 26-letter string: ^ [A-Za-z] + $

Verify the string of 26 uppercase letters: ^ [Amurz] + $

Verify the string of 26 lowercase letters: ^ [amurz] + $

Verify the string consisting of numbers and 26 letters: ^ [A-Za-z0-9] + $

Verify a string consisting of numbers, 26 letters, or an underscore: ^\ wicked $

Validating user names or nicknames is often used: ^ [\ u4e00 -\ u9fa5A-Za-z0-9Mui _] * $can only be in Chinese and English, numbers, underscores, minus signs

Verify the user password: ^ [a-zA-Z]\ w {5pm 17} $is in the correct format: begins with a letter, is between 6 and 18 in length, and can only contain characters, numbers, and underscores.

Verify whether it contains characters such as ^% &',; =? $\ ": [^% &',; =? $\ x22] +

Verify Chinese characters: ^ [\ u4e00 -\ u9fa5], {0,} $

Verify the Email address: ^\ w + [- +.]\ w +) * @\ w + ([-.]\ w +) *.\ w + ([-.]\ w +) * $

Verify InternetURL: ^ http://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$; ^ [a-zA-z] +: / / (w + (- w +) *) (. (W + (- w +) *)) * (? S*)? $

Verify the phone number: ^ (\ (\ d {3pr 4}\) |\ d {3pr 4} -)?\ d {7J 8} $:-- the correct format is: XXXX-XXXXXXX,XXXX-XXXXXXXX,XXX-XXXXXXX,XXX-XXXXXXXX,XXXXXXX,XXXXXXXX.

Verify ID number (15 or 18 digits): ^\ d {15} |\ d {} 18 $

Verify that the 12 months of the year: ^ (0? [1-9] | 1 [0-2]) $is in the correct format: "01"-"09" and "1"12"

Verify the 31 days of a month: ^ ((0? [1-9]) | (1 | 2) [0-9]) | 30 | 31) the correct format is: 01, 09 and 1, 31.

Integer: ^ -?\ dbath $

Non-negative floating point number (positive floating point number + 0): ^\ d + (\.\ d +)? $

Positive floating point number ^ (([0-9] +. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *)) $

Non-positive floating point number (negative floating point number + 0) ^ ((-\ d + (\.\ d +)?) | (0 + (\ .0 +)?)) $

Negative floating point number ^ (- ([0-9] +\. [0-9] * [1-9] [0-9] *) | ([0-9] * [1-9] [0-9] *. [0-9] +) | ([0-9] * [1-9] [0-9] *)) $

Floating point number ^ (-?\ d +) (\.\ d +)? $

Due to the continuous updating of the mobile phone number range, the previous regular expression has been unable to meet the demand. Rewrite this expression

Var regex = {mobile: / ^? ([-] | []) [-] {} $/}

Expression parsing:

"/" represents a regular expression.

"^" represents the beginning of the string, and "$" represents the end of the string.

"?" It means to match one or zero of the preceding characters, so here 0? It means that the mobile phone number can start with 0 or not.

The next part verifies the 11-digit mobile phone number, starting from 13, because the optional range from 130 to 139 is [0-9], and the number that begins with 15 does not have 154, so there is no 4 in [], of course, it can also be written as [0-35-9]. The numbers opened by 18 and 14 below are the same as above.

The parentheses represent a subexpression in which four optional branches are distinguished by "|". In the rule, the priority of "|" is the lowest. Here, each branch matches 3 characters (a [] can only match one character, which means optional), that is, the first three digits of the mobile phone number, so there are still 8 digits to match. It can be any character of 0-9, so it is "[0-9] {8}", and the number in {} represents the number of matching previous characters.

Thank you for reading, the above is the content of "JavaScript regular expression verification code", after the study of this article, I believe you have a deeper understanding of the problem of JavaScript regular expression verification code, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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