JavaScript Regular Expression Verification Code (recommended) and javascript Regular Expression
RegExp: Short for a regular expression (regular expression.
Regular expressions describe character pattern objects. You can use a regular expression to describe the content to be retrieved.
A simple mode can be a single character. More complex modes include more characters and can be used for parsing, format check, replacement, and so on.
// Determine whether the input content is empty function IsNull () {var str = document. getElementById ('str '). value. trim (); if (str. length = 0) {alert ('Sorry, the text box cannot be blank or blank! '); // Change the "text box" to the attribute name you want to verify!} // Judge whether the date type is type in YYYY-MM-DD format function IsDate () {var str = document. getElementById ('str '). value. trim (); if (str. length! = 0) {var reg =/^ (\ d {}) (-| \/) (\ d {}) \ 2 (\ d }) $/; var r = str. match (reg); if (r = null) alert ('Sorry, the date format you entered is incorrect! '); // Change "date" to the name of the property to be verified!} // Determine whether the date type is YYYY-MM-DD hh: mm: ss format type function IsDateTime () {var str = document. getElementById ('str '). value. trim (); if (str. length! = 0) {var reg =/^ (\ d {}) (-| \/) (\ d {}) \ 2 (\ d }) (\ d {1, 2}) :( \ d {1, 2}) :( \ d {1, 2}) $/; var r = str. match (reg); if (r = null) alert ('Sorry, the date format you entered is incorrect! '); // Change "date" to the name of the property to be verified!} // Determine whether the date type is hh: mm: ss type function IsTime () {var str = document. getElementById ('str '). value. trim (); if (str. length! = 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 format you entered is incorrect! "); // Change" date "to the attribute name you want to verify! }}}// Determines whether the entered character is an English letter function IsLetter () {var str = document. getElementById ('str '). value. trim (); if (str. length! = 0) {reg =/^ [a-zA-Z] + $/; if (! Reg. test (str) {alert ("sorry, the English letter format you entered is incorrect! "); // Change" English letter type "to the attribute name you want to verify! }}}// Determines whether the input character is an integer function IsInteger () {var str = document. getElementById ('str '). value. trim (); if (str. length! = 0) {reg =/^ [-+]? \ D * $/; if (! Reg. test (str) {alert ("sorry, the format of the integer type you entered is incorrect! "); // Replace" Integer type "with the property name you want to verify! }}}// Determines whether the input character is double precision function IsDouble (val) {var str = document. getElementById ('str '). value. trim (); if (str. length! = 0) {reg =/^ [-\ +]? \ D + (\. \ d + )? $/; If (! Reg. test (str) {alert ("sorry, the format of the Double Precision type you entered is incorrect! "); // Replace" double-precision type "with the property name you want to verify!} // Determine whether the input character is: a-z, A-Z, 0-9 function IsString () {var str = document. getElementById ('str '). value. trim (); if (str. length! = 0) {reg =/^ [a-zA-Z0-9 _] + $/; if (! Reg. test (str) {alert ("sorry, the format of the string type you entered is incorrect! "); // Replace" string type "with the property name you want to verify! }}}// Determine whether the input character is Chinese function IsChinese () {var str = document. getElementById ('str '). value. trim (); if (str. length! = 0) {reg =/^ [\ u0391-\ uFFE5] + $/; if (! Reg. test (str) {alert ("sorry, the format of the string type you entered is incorrect! "); // Replace" string type "with the property name you want to verify! }}} // Determine whether the input EMAIL format is correct function IsEmail () {var str = document. getElementById ('str '). value. trim (); if (str. length! = 0) {reg =/^ \ w + ([-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * $/; if (! Reg. test (str) {alert ("sorry, the format of the string type you entered is incorrect! "); // Replace" string type "with the property name you want to verify! }}} // Determine whether the entered zip code (only six digits can be entered) is correct function IsZIP () {var str = document. getElementById ('str '). value. trim (); if (str. length! = 0) {reg =/^ \ d {6} $/; if (! Reg. test (str) {alert ("sorry, the format of the string type you entered is incorrect! "); // Replace" string type "with the property name you want to verify! }}} // Judge whether the input number is greater than a specific number. function MaxValue () {var val = document. getElementById ('str '). value. trim (); if (str. length! = 0) {reg =/^ [-+]? \ D * $/; if (! Reg. test (str) {// determine whether it is a numeric type if (val> parseInt ('000000') // "123" is the maximum value set by yourself {alert ('Sorry, the number you entered is out of the range '); // change "Number" to the attribute name you want to verify! }}}}
Phone:/^ (\ d {2, 3} \) | (\ d {3 }\-))? (\ (0 \ d {2, 3} \) | 0 \ d {2, 3 }-)? [1-9] \ d {6, 7} (\-\ d {1, 4 })? $/
Mobile:/^ (\ d {2, 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 {4, 8} $/
A special amount:/^ (\ d {1, 3} (, \ d {3}) *) | (\ d +) (\. \ d {2 })? $ // Description: Except for the format "xxx xx, xxx xx, XXX.00"
// Provide various JS verification methods. trim () attribute String. prototype. trim = function () {return this. replace (/(^ \ s *) | (\ s * $)/g ,"");}
Call:
<Input type = "text" name = "str"> <input type = "button" value = "OK" onClick = ""> // write the JS you want to call in onClick. verification function <script language = "javascript" type = "text/javascript"> var patterms = new Object (); // verify IPpatterms. ip =/^ (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) (\. (\ d {1, 2} | 1 \ d | 2 [0-4] \ d | 25 [0-5]) {3} $ /; // verify EMAILpatterms. email =/^ [a-zA-Z0-9 _-] + @ [a-zA-Z0-9 _-] + (\. [a-zA-Z0-9 _-] +) + $/; // validate Date Format 2009-07-13patterms.date =/^ \ d {4}-(0? [1-9] | 1 [0-2])-(0? [1-9] | [1-2] \ d | 3 [0-1]) $ // The verification time format is 16: 55: 39patterms. time = new RegExp ("^ ([0-1] \ d | 2 [0-3]): [0-5] \ d: [0-5] \ d $ "); // verify the 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 ", "date") + "," + verify ("192.168", "ip "));
Verification number: ^ [0-9] * $
Verify the n-digit number: ^ \ d {n} $
Verify at least n digits: ^ \ d {n,} $
Verify m-n digits: ^ \ d {m, n} $
Verify the number starting with zero or zero: ^ (0 | [1-9] [0-9] *) $
Verify the positive number of two decimal places: ^ [0-9] + (. [0-9] {2 })? $
Verify the positive number of 1-3 decimal places: ^ [0-9] + (. [0-9] {1, 3 })? $
Verify a non-zero positive integer: ^ \ +? [1-9] [0-9] * $
Verify a non-zero negative integer: ^ \-[1-9] [0-9] * $
Verify non-negative integer (positive integer + 0) ^ \ d + $
Verify non-positive integer (negative integer + 0) ^ (-\ d +) | (0 +) $
3 characters for verification: ^. {3} $
Verify A string consisting of 26 English letters: ^ [A-Za-z] + $
Verify a string consisting of 26 uppercase letters: ^ [A-Z] + $
Verify a string consisting of 26 lower-case letters: ^ [a-z] + $
Verify a string consisting of digits and 26 English letters: ^ [A-Za-z0-9] + $
Verify a string consisting of digits, 26 English letters, or underscores: ^ \ w + $
Often used to verify the user name or nickname: ^ [\ u4e00-\ u9fa5A-Za-z0-9-_] * $ only Chinese and English, numbers, underscores, minus signs
Verify User Password: ^ [a-zA-Z] \ w {5, 17} $ the correct format is: it must start with a letter and be between 6 and 18 characters. It can only contain characters, numbers, and underscores.
Check whether ^ % & ',; =? $ \ "And other characters: [^ % & ',; =? $ \ X22] +
Verify Chinese characters: ^ [\ u4e00-\ u9fa5], {0,} $
Verify Email address: ^ \ w + [-+.] \ w +) * @ \ w + ([-.] \ w + )*\. \ w + ([-.] \ w +) * $
Verify InternetURL: ^ http: // ([\ w-] + \.) + [\ w-] + (/[\ w -./? % & =] *)? $; ^ [A-zA-z] +: // (w + (-w +) *) (. (w + (-w + )*))*(? S *)? $
Verification phone number: ^ (\ d {3, 4} \) | \ d {3, 4 }-)? \ D {7,8} $: -- the correct format is: XXXX-XXXXXXX, XXXX-XXXXXXXX, XXX-XXXXXXX, XXX-XXXXXXXX, XXXXXXX, XXXXXXXX.
Verify the ID card number (15 or 18 digits): ^ \ d {15} | \ d {} 18 $
12 months of verification: ^ (0? [1-9] | 1 [0-2]) $ the correct format is: "01"-"09" and "1" "12"
31 days of verification for a month: ^ (0? [1-9]) | (1 | 2) [0-9]) | 30 | 31) $ the correct format is: 01, 09, 1, 31.
Integer: ^ -? \ D + $
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 + )? $
As mobile phone numbers are constantly updated, the previous regular expressions cannot meet the requirements. Rewrite this expression,
var regex = {mobile: /^?([-]|[]|[]|[])[-]{}$/}
Expression Analysis:
"/" Represents a regular expression.
"^" Indicates the start position of the string, and "$" indicates the end position of the string.
"?" It indicates matching one or zero characters before, so here 0? The mobile phone number can start with 0 or not.
Next, verify the 11-digit mobile phone number starting from 13. The range is [0-9] Because the 11-digit mobile phone number is available from 130-139. the number starting with 15 is not 154, so [] does not contain the number 4. Of course, it can also be written as [0-35-9]. The numbers listed in the 18 and 14 below are the same as those listed in the preceding figure.
Enclosed in parentheses represents a subexpression, which contains four optional branches separated by "|". In regular expressions, "|" has the lowest priority, here, each branch matches three characters (one [] can only match one character, which is optional), that is, the first three digits of the mobile phone number, then there are 8 digits to be matched, which can be any 0-9 character. Therefore, the value is "[0-9] {8}", and the number in {} indicates the number that matches the previous character.
The above content is the JavaScript Regular Expression Verification Code shared by Alibaba Cloud. I hope it will be helpful to you. If you want to learn more exciting content, please stay tuned to the help House website. Thank you!