Regular expression-Registry validation __javascript

Source: Internet
Author: User
Regular Expressions-Registry validation

Regular Expressions
A regular expression is a logical formula for string manipulation, which is a "rule string" that is used to express a filtering logic for a string, using a predefined set of characters and a combination of those particular characters.

The purpose of the regular expression
1. Whether the given string conforms to the filter logic of the regular expression (called "match");
2. A regular expression can be used to get the specific part we want from the string.

regular expressions are characterized by
1. Flexibility, logic and functionality are very strong;
2. The complex control of strings can be achieved quickly and in a very simple way;
3. For those who have just contacted, it is more obscure and difficult to understand.

Registry Validation

1. Get ID

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

2. Verify Name

function CheckName () {
    //Get value
    var username=$ (' user '). Value;
    The judge cannot be an empty
    if (username== ') {
        $ (' s1 '). innerhtml= ' username cannot be empty ';
        return false;
    }
    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 5-10 digits or letters, the beginning cannot be a number ';
        return false;
    }
    Match, return the empty
        $ (' s1 '). innerhtml= ';
        return true;
}
Note: S1 to judge the contents of the prompt, add <span id= ' s1 ' after the input box

3. Verify Password

function Checkpwd () {
    //Get value
        ditto ...
    Judge can not be empty ibid ...
    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 ';
        return false;
    }
    Match, return null
        ibid ...
}

4. Verify the Mailbox

function Checkemail () {
    //Get value
        ditto ...
    Judge can not be empty ibid ...
    Regular expression
    var reg=/^\w+@\w+\.com|cn|net$/;           
    Detects if the input matches the regular expression
    if (!reg.test (email)) {
        $ (' S3 '). innerhtml= ' mailbox illegal ';
        return false;
    }
    Match, return null
        ibid ...
}

5. Verify mobile phone number

function Checktel () {
    //Get value
        ditto ...
    Judge can not be empty ibid ...
    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 illegal ';
        return false;
    }
    Match, return null
        ibid ...
}

6. Verify the ID number

function Checkcid () {
    //Get value
        ditto ...
    Judge can not be empty ibid ...
    Regular expression
    var reg=/^\d{15}$|^\d{17}\d|x$/;         
    Detects if the input matches the regular expression
    if (!reg.test (CID)) {
        $ (' S5 '). innerhtml= ' id not legal ';
        return false;
    }
    Match, return null
        ibid ...
}

7. Verify QQ number

function Checkqq () {
    //Get value
        ditto ...
    Judge can not be empty ibid ...
    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 consist of 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;
    }

Of course, the jquery Validate validation framework can be used to express validation in the later stages, making it more convenient.

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.