JS Regular Expression validation

Source: Internet
Author: User
Tags uppercase letter

Added 150,153,156,158,159,157,188,189
The regular expression is as follows: ^ (13[0-9]|15[0|3|6|7|8| 9]|18[8|9]) \d{8}$//checksum is all made up of numbers
function IsDigit (s)
{
var patrn=/^[0-9]{1,20}$/;
if (!patrn.exec (s)) return false
return True
}

Check login name: Only 5-20 entries begin with a letter, can be numbered, "_", "." The string
function Isregisterusername (s)
{
var patrn=/^[a-za-z]{1} ([a-za-z0-9]|[. _]) {4,19}$/;
if (!patrn.exec (s)) return false
return True
}

Verify user name: Only 1-30 strings beginning with a letter can be entered
function Istruename (s)
{
var patrn=/^[a-za-z]{1,30}$/;
if (!patrn.exec (s)) return false
return True
}

Check password: Only 6-20 letters, numbers, underscores can be entered
function ispasswd (s)
{
var patrn=/^ (/w) {6,20}$/;
if (!patrn.exec (s)) return false
return True
}

Check the ordinary telephone, fax number: Can "+" start, in addition to the number, can contain "-"
function Istel (s)
{
var patrn=/^[+]{0,1} (/d) {1,3}[]? ([-]? (/d) {1,12}) +$/;
var patrn=/^[+]{0,1} (/d) {1,3}[]? ([-]? ((/d) | []) {1,12}) +$/;
if (!patrn.exec (s)) return false
return True
}

Check mobile phone Number: Must start with a number, except the number, can contain "-"
function Ismobil (s)
{
var patrn=/^[+]{0,1} (/d) {1,3}[]? ([-]? ((/d) | []) {1,12}) +$/;
if (!patrn.exec (s)) return false
return True
}

Verifying ZIP Codes
function Ispostalcode (s)
{
var patrn=/^[a-za-z0-9]{3,12}$/;
var patrn=/^[a-za-z0-9]{3,12}$/;
if (!patrn.exec (s)) return false
return True
}

Verifying search Keywords
function Issearch (s)
{
var patrn=/^[^ ' [email protected]#$%^&* () +=|///][/]/{/}:; ' /,.<>/?] {1} [^ ' [email protected]$%^& () +=|///] [/]/{/}:;‘ /,.<>?] {0,19}$/;
if (!patrn.exec (s)) return false
return True
}

function IsIP (s)//by zergling
{
var patrn=/^[0-9.] {1,20}$/;
if (!patrn.exec (s)) return false
return True
}


Regular expressions
"^//d+$"//nonnegative integer (positive integer + 0)
"^[0-9]*[1-9][0-9]*$"//Positive integer
"^ ((-//d+) | (0+)) $ "//non-positive integer (negative integer + 0)
"^-[0-9]*[1-9][0-9]*$"//Negative integer
"^-?//d+$"//Integer
"^//d+ (//.//d+)? $"//non-negative floating-point number (positive floating point + 0)
^ ([0-9]+//]. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*//. [0-9]+) | ([0-9]*[1-9][0-9]*)) $ "//positive floating-point number
"^ ((-//d+ (//.//d+)?) | (0+ (//.0+)?)) $ "//non-positive floating-point number (negative floating-point number + 0)
^ (-([0-9]+//]. [0-9]*[1-9][0-9]*) | ([0-9]*[1-9][0-9]*//. [0-9]+) | ([0-9]*[1-9][0-9]*))) $ "//negative floating-point number
^ (-?//d+) (//.//d+)? $ "//floating-point number
"^[a-za-z]+$"//A string consisting of 26 English letters
"^[a-z]+$"//A string consisting of 26 uppercase letters in English
"^[a-z]+$"//String consisting of 26 English letters in lowercase
"^[a-za-z0-9]+$"//string consisting of a number and 26 English letters
"^//w+$"//A string consisting of numbers, 26 letters or underscores
"^[//w-]+ (//.[ w-]+) *@[//w-]+ (//.[ w-]+) +$ "//email address
"^[a-za-z]+://(//w+ (-//w+) *) (//. ( w+ (-//w+) *) * (//?//s*)? $ "//url

"^[a-za-z0-9_]*$"


The use of regular expressions is detailed
Regular expressions are generally in the following form:
/love/the part where the "/" delimiter is located is the pattern that will be matched in the target object. The user simply puts the pattern content that you want to find the matching object in between the "/" delimiter. To enable the user to customize the schema content more flexibly, the regular expression provides a special "meta-character". A meta-character is a special character in a regular expression that can be used to specify its leading character (that is, the character in front of the metacharacters) in the target object.
The more commonly used metacharacters are: "+", "*", and "?".
The "+" metacharacters stipulate that their leading characters must appear one or more times in the target object.
The "*" meta-character specifies that its leading character must appear 0 or more times in the target object.
“?” A meta-character specifies that its leading object must appear 0 or more times in the target object.
Below, let's look at the specific application of the regular expression meta-character.
/fo+/because the preceding regular expression contains the "+" metacharacters, it can be matched with a string of "fool", "fo", or "football" in the target object, and so on with one or more letters O consecutively after the letter F.
/eg*/because the preceding regular expression contains the "*" metacharacters, it can be matched with a string of "easy", "ego", or "egg" in the target object, such as 0 or more consecutive letters G after the letter E.
/wil?/because the preceding regular expression contains the "? A meta-character that represents a string that can match "Win" in the target object, or "Wilson", and so on, after the letter I, 0 consecutive or one letter L.
Sometimes you don't know how many characters to match. To be able to adapt to this uncertainty, the regular expression supports the concept of qualifiers. These qualifiers can specify how many times a given component of a regular expression must appear to satisfy a match.
{n} n is a non-negative integer. Matches the determined n times. For example, ' o{2} ' cannot match ' o ' in ' Bob ', but can match two o in ' food '.
{N,} n is a non-negative integer. Match at least n times. For example, ' o{2,} ' cannot match ' o ' in ' Bob ', but can match all o in ' Foooood '. ' O{1,} ' is equivalent to ' o+ '. ' O{0,} ' is equivalent to ' o* '.
{n,m} m and n are non-negative integers, where n <= m. Matches at least n times and matches up to M times. For example, "o{1,3}" will match the first three o in "Fooooood". ' o{0,1} ' is equivalent to ' O? '. Note that there can be no spaces between a comma and two numbers.

In addition to metacharacters, users can specify exactly how often the pattern appears in the matching object. For example,/jim {2,6}/The regular expression above specifies that the character m can appear consecutively 2-6 times in a matching object, so the above regular expression can match a string such as Jimmy or Jimmmmmy.
After a preliminary understanding of how to use regular expressions, let's take a look at some of the other important metacharacters uses.
/s: Used to match a single space character, including Tab key and line feed;
/S: Used to match all characters except a single space character;
/d: Used to match numbers from 0 to 9;
/w: Used to match letters, numbers, or underscore characters;
/w: Used to match all characters that do not match the/w;
. : Used to match all characters except the line break.
(Note: We can think of S/s and/w and/w as mutual inverse)
Below, we'll look at how to use the above metacharacters in regular expressions using an example.
s+/the preceding regular expression can be used to match one or more space characters in the target object.
D000/If we have a complex financial statement in hand, we can easily find all sums amounting to thousands of dollars through these regular expressions.
In addition to the meta-characters we have described above, there is another unique special character in the regular expression, the locator. The locator is used to specify where the matching pattern appears in the target object. The more commonly used locators include: "^", "$", "/b", and "/b".
The "^" locator specifies that the matching pattern must be at the beginning of the target string
The "$" locator specifies that the matching pattern must be at the end of the target object
The "/b" locator specifies that the matching pattern must be one of the two boundaries at the beginning or end of the target string.
The "/b" locator specifies that the matching object must be within the first and end two boundaries of the target string, that is, the matching object is neither the beginning of the target string nor the end of the target string. Similarly, we can think of "^" and "$" as well as "/b" and "B/S" as two sets of locators for reciprocal operations. For example:/^hell/because the preceding regular expression contains a "^" Locator, you can match a string that begins with "Hell", "Hello" or "Hellhound" in the target object. /ar$/because the above regular expression contains a "$" locator, you can match a string that ends with "car", "bar" or "AR" in the target object. bbom/because the preceding regular expression pattern begins with a "/b" locator, you can match a string that starts with "bomb" or "BOM" in the target object. /man/b/because the preceding regular expression pattern ends with a "/b" locator, you can match a string that ends with "human", "Woman" or "man" in the target object.
In order to make it easier for users to set the matching pattern, the regular expression allows the user to specify a range in the matching pattern rather than a specific character. For example:
/[a-z]/the above regular expression will match any uppercase letter from a to Z range.
/[a-z]/the above regular expression will match any lowercase letter from a to Z range.
/[0-9]/the above regular expression will match any number in the range from 0 to 9.
/([a-z][a-z][0-9]) +/the above regular expression will match any string consisting of letters and numbers, such as "aB0". One thing you should be reminded of here is that you can use "()" in regular expressions to group strings together. The "()" symbol contains content that must appear in the target object at the same time. Therefore, the preceding regular expression will not match a string such as "ABC", because the last character in "ABC" is a letter rather than a number.
If we want to implement a "or" operation in a regular expression similar to a programming logic, you can use the pipe symbol "|" In any of several different patterns to match. For example:/to|too|2/the above regular expression will match "to", "too", or "2" in the target object.
There is also a more commonly used operator in the regular expression, the negative character "[^]". Unlike the locator "^" we described earlier, the negation "[^]" specifies that a string specified in the pattern cannot exist in the target object. For example:/[^a-c]/the above string will match any character except A, B, and C in the target object. In general, "^" is considered a negation operator when it appears in "[]", and when "^" is outside of "[]" or "[]", it should be treated as a locator.
Finally, the escape character "/" can be used when the user needs to include metacharacters in the pattern of the regular expression and find the matching object. For example:/th/*/the above regular expression will match the "th*" in the target object, not the "the", and so on.
After the regular expression is constructed, it can be evaluated like a mathematical expression, that is, it can be evaluated from left to right and in a priority order. The priority levels are as follows:
1. /Escape character
2. (), (?:), (? =), [] parentheses and square brackets
3. *, +,?, {n}, {n,}, {n,m} qualifier
4. ^, $,/anymetacharacter position and order
5.| " or the action

JS Regular Expression validation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.