Share regular expression registry validation and some common validation examples

Source: Internet
Author: User
Tags validation examples
The regular expression looks like a big push symbol, makes people very big, I want to believe that many people just come to see when the headache, I also. But not to learn and not to, this is a very important thing, today here is a summary of some of the sharing of the regular expression registry validation and some common validation examples.

Regular expressions

A regular expression is a logical formula for a string operation, which is a "rule string" that is used to express a filter logic for a string, using predefined specific characters and combinations of those specific characters.

The purpose of regular expressions

1. Whether the given string conforms to the filtering logic of the regular expression (called "match");

2. You can get the specific part we want from the string using a regular expression.

The regular expression is characterized by

1. Flexibility, logic and functionality are very strong;

2. Complex control of strings can be achieved quickly and in a very simple way;

3. For people who have just come into contact, it is more obscure and difficult to understand.

I tasted all day and thought is better than a moment of learning, so learn this to persevere.

Registry validation

1. Get ID

function $ (ID) {  return document.getElementById (ID);}

2. Verify your Name


function CheckName () {  //Gets the value  var username=$ (' user '). Value;  The judgment cannot be null  if (username== ') {    $ (' s1 '). innerhtml= ' username cannot be empty ';    return false;  }  The regular expression  var reg=/^[a-za-z][a-za-z0-9]{4,9}$/;  Detects if the input matches the regular expression  if (!reg.test (username)) {    $ (' s1 '). innerhtml= ' username must be a 5-10-digit number or letter consisting of a number at the beginning;    return false;  }  Match, return empty    $ (' s1 '). innerhtml= ';    return true;}

Note: S1 for judging the content, add in the input box after <span id= ' s1 ' ></span>

3. Verify the password


function Checkpwd () {  //Get value    ditto  ... Judgment cannot be empty    Ibid  ... The regular expression  var reg=/^\s{6,}$/;  Detects if the input matches the regular expression  if (reg.test (password) ==false) {    $ (' s2 '). innerhtml= ' Password must be more than 6 bits ';    return false;  }  Match, return null    ibid ...}

4. Verify the Mailbox


function Checkemail () {  //Get value    ditto  ... Judgment cannot be empty    Ibid  ... The regular expression  var reg=/^\w+@\w+\.com|cn|net$/;        Detects if the input matches the regular expression  if (!reg.test (email)) {    $ (' S3 '). Innerhtml= ' email is illegal ';    return false;  }  Match, return null    ibid ...}

5. Verify the phone number


function Checktel () {  //Get value    ditto  ... Judgment cannot be empty    Ibid  ... The regular expression  var reg=/^1[34578]\d{9}$/;       Detects if the input matches the regular expression  if (!reg.test (tel)) {    $ (' S4 '). Innerhtml= ' mobile phone number is not legal ';    return false;  }  Match, return null    ibid ...}

6. Verify the ID number

function Checkcid () {  //Get value    ditto  ... Judgment cannot be empty    Ibid  ... The regular expression  var reg=/^\d{15}$|^\d{17}\d|x$/;       Detects if the input matches the regular expression  if (!reg.test (CID)) {    $ (' S5 '). Innerhtml= ' identity card is not legal ';    return false;  }  Match, return null    ibid ...}

7. Verify QQ number

function Checkqq () {  //Get value    ditto  ... Judgment cannot be empty    Ibid  ... The regular expression  var reg=/^[1-9]\d{7,10}$/;       Detects if the input matches the regular expression  if (!reg.test (QQ)) {    $ (' S6 '). Innerhtml= ' QQ must be 8 to 11 digits, the beginning cannot be 0 ';    return false;      }  Match, return null    ibid ...}

8. Detect all Conditions

function Checkall () {  if (CheckName () &&checkpwd () &&checkemail () &&checktel () & &checkcid () &&checkqq ()) {    return true;  } else{    return false;  }}

Common regular-expression validation

    1. Verify Password strength

The strength of the password must be a combination of uppercase and lowercase letters and numbers, no special characters, and a length of 8-10.

/^ (? =.*\\d) (? =.*[a-z]) (? =.*[a-z]). {8,10}$/

2. Verifying Chinese

The string can only be in Chinese.

/^[\\u4e00-\\u9fa5]{0,}$/

3. A string of numbers, 26 English letters, or underscores

/^\\w+$/

4. Verify the e-mail address

As with the password, here is the regular check statement for e-mail address compliance.

/[\\w!#$%& ' *+/=?^_ ' {|} ~-]+(?:\ \. [\\w!#$%& ' *+/=?^_ ' {|} ~-]+) *@ (?: [\\w] (?: [\\w-]*[\\w])? \ \.) +[\\w] (?: [\\w-]*[\\w])?/

5. Check the ID number

The following is a regular check of the ID number. 15 or 18 bits.

15-bit:

/^[1-9]\\d{7} ((0\\d) | ( 1[0-2]) (([0|1|2]\\d) |3[0-1]) \\d{3}$/

18-bit:

/^[1-9]\\d{5}[1-9]\\d{3} ((0\\d) | ( 1[0-2]) (([0|1|2]\\d) |3[0-1]) \\d{3} ([0-9]| X) $/

6. Check the date

Date checksum in "YYYY-MM-DD" format, which has been considered for a flat leap year.

/^(?:(?! 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) $/

7. Check the amount

The amount of the check, accurate to 2 decimal places.

/^[0-9]+ (. [ 0-9]{2})? $/

8. Check the phone number

The following is the domestic mobile phone number 13, 15, 18, the regular expression. (Can be expanded according to the current domestic collection number of the first two-digit opening 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}$/

9. Determine the version of IE

IE has not been completely replaced, many pages still need to do version compatibility, the following is the version of IE to check the expression.

/^.*msie[5-8] (?: \ \. [0-9]+]? (?!. *trident\\/[5-9]\\.0). *$/

10. Verifying the IP-V4 Address

IP4 the regular statement.

/\\b (?:(? : 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]?) \\b/
Related Article

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.