JavaScript: Regular expressions, an example of a form validation

Source: Internet
Author: User
Tags modifier modifiers ranges

Read Catalogue

    • The content of this article:
    • Regular Expressions:
    • Examples of forms validation using regular expressions:

Back to top of this article:

    • Regular expressions
      • How to use regular expressions
      • Special match characters for regular expressions
      • Regular expression modifiers
    • Examples of forms validation using regular expressions

Starting Date: 2018-05-13

Go back to the top regular expression:

How to use regular expressions:
    1. The regular Expression object is created first: "character rules for regular expressions: if there is no special meaning for the character, write directly, if there is a special meaning, write directly, if you want to turn special meaning characters into non-special meaning, use the"
      1. You can create regular expression objects from REGEXP objects: var variable name =new RegExp ("expression" [, modifier])
      2. You can also create a regular expression object "called literal Creation" by using a combination of characters in a certain format (beginning and ending with a slash (/)): The var variable name =/expression/modifier "Regular expression modifier is written in the regular expression terminator/After, is optional, defines some rules such as matching ignoring case" 】
    2. The second string uses regular expressions to get the matching result:
      1. Use the method provided by the regular object:
        1. Re.test (String): Returns TRUE if compliant, otherwise false
        2. Re.exec (String): does not conform to return null, can be found to return the matching character and start position
      2. Use the method provided by the string object:
        1. Search (Regular expression): Finds a string that conforms to a regular expression, returns the position where the string started, and returns 1 if no matching substring is found.
        2. Match (regular expression): Finds a string that conforms to a regular expression, returns a string that matches the result, and if no matching text is found, match () returns null
        3. Replace (regular expression, string to replace): finds a string that conforms to a regular expression, then replaces it with another string, returns the replaced string, and returns the original string if no lookup succeeds.
        4. Split (regular expression): Finds a string that conforms to a regular expression, and then splits the entire string according to it. The returned result is multiple strings.

Special match characters for regular expressions:
Character Significance
\ Masking the special meaning of using characters, such as $ means not to use $ to match, and just treat it as a normal character
^ The character after which the ^ followed must be the beginning of the string
$ The character representing the front of $ must be the end of the string
* Match * preceding characters 0 or more times (greedy, the more the more)
+ Match + previous characters 1 or more times (greedy and the more)
? Match the preceding character 0 or 1 times
. Match all characters except the line break \ n
\d Match all the digits of the 0~9 once
\d Match all characters of non-digits once
\s Match a null character, such as line break, space, indent
\w Match any letters, numbers, and underscores
\w Match characters other than numbers, letters, and underscores
[0-9] Match a number from 0 to 9 once
[A-z] Match letters from A to Z once
[several letters], such as [ABCD] Match any one of the letters in [] once (priority from left to right)
[A range or several ranges], e.g. [a-z0-9] Match [] One character in several ranges at a time (left to right precedence)
[^ Range] Match characters that are not in range
N matches the preceding character n times
{N,} Matches the preceding character at least n times
{N,m} Matches the preceding character n~m times
X|y Match x or Y, (left to right priority)
(A string of special characters) Match a string of special characters to a group

Regular expression modifiers

(written in the regular expression terminator/back,):

Character Significance
G Global match, not just one match
I Match ignores character case

After using g, for exec can be repeated execution to get the results, for the method provided by the string, will return multiple results at once:

Back to top examples of form validation using regular expressions:

This is a simple example, just do the matching action, do not effect, this is just a small shelf, want to increase the effect can be increased by themselves.

Preparatory work:

A regular expression to match the mailbox:/^ ([\w-]+ (?: \. [\w-]+] @ ((?: [\w-]+\.) *\W[\W-]{0,66}) \. ([a-z]{2,6} (?: \. [A-z] {2})?) $/i

A regular expression to match the world mobile phone number:/^ (13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8| 9]|18[0|1|2|3|5|6|7|8| 9]) \d{8}$/

A regular expression that matches the password (the length is between 6~18 and can contain only letters and numbers):/^[a-za-z0-9]{6,18}$/

Get Value:

To create a form, bind the trigger function:
    • The action points to a nonexistent URL and does not jump if the commit fails

If all three input boxes match the criteria, then return true to allow the Sumbit event to go on and jump in, and if any one does not meet the criteria, then return FALSE to prevent the submit event from being

<!    DOCTYPE html>

JavaScript: Regular expressions, an example of a form 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.