JS regexp simple phone and mailbox regular

Source: Internet
Author: User

To create the syntax for a RegExp object:

var obj=new RegExp (pattern,attributes);

The code is as follows Copy Code

Case 1:


var pattern=/a/;
document.write (Pattern.exec ("Asdas"));

Case 2:


var obj=new RegExp ("a");
document.write (Obj.exec ("Asdas"));


The execution results of Case 1 and Case 2 are the same;

Parameters

Parameter pattern is a string that specifies the schema or other regular expression of the regular expression.

The parameter attributes is an optional string that contains the property "G" (performs a global match lookup for all matches rather than stopping after the first match is found.) , "I": Performs a case-insensitive match. and "M": Perform multiple line matches. , used to specify global matches, case-sensitive matches, and multiple line matches, respectively. The M attribute is not supported until ECMAScript is standardized. If pattern is a regular expression, rather than a string, you must omit the argument.

return value

A new RegExp object with the specified pattern and flags. If parameter pattern is a regular expression instead of a string, the RegExp () constructor creates a new RegExp object with the same pattern and flags as the specified REGEXP.

If you do not use the new operator and REGEXP () as a function call, its behavior is the same as when called with the new operator, except that when pattern is a regular expression, it returns only the formula and no longer creates a new RegExp object.

Thrown
SyntaxError-Throws an exception if the pattern is not a valid regular expression, or if the attributes contains characters other than "G", "I", and "M".

TypeError-This exception is thrown if pattern is a RegExp object, but the attributes argument is not omitted.
RegExp Object Method:
1. Test (): Retrieves the value specified in the string. Returns TRUE or FALSE.

The code is as follows Copy Code
var patt1=new RegExp ("E");
document.write (Patt1.test ("The best things in life are Free");

2, EXEC (): Retrieves the value specified in the string. Returns the found value and determines its location.

Method retrieves the specified value in a string. The return value is the value that was found. If no match is found, NULL is returned.

3, compile (): You can change the retrieval mode, you can add or delete the second parameter.

The code is as follows Copy Code

var patt1=new RegExp ("E");
document.write (Patt1.test ("The best things in life are Free");
Patt1.compile ("D");
document.write (Patt1.test ("The best things in life are Free");

There are a lot of scenarios under this scenario, and sometimes the username must be a cell phone or a mailbox, and the other is wrong.

In fact, this regular expression is also relatively simple, I will explain the following:

The regular of the mailbox:

The code is as follows Copy Code

^[\w.\-]+@ (?: [a-z0-9]+ (?:-[a-z0-9]+) *\.) +[a-z]{2,3}$

The regular of the mobile phone

The code is as follows Copy Code

^1[3|4|5|8]\d{9}$

Two regular combinations, only need "I" on it, as follows:

The code is as follows Copy Code
(^[\w.\-]+@ (?: [a-z0-9]+ (?:-[a-z0-9]+) *\.) +[a-z]{2,3}$) | (^1[3|4|5|8]\d{9}$)//email and cell phone

In the same way, sometimes when filling in the logistics information, need to leave contact, contact can be mobile phone or telephone, then the same is also very simple, you can write as follows:

The code is as follows Copy Code
(^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$) | (^0 (10|2[0-5789]|\\d{3}) \\d{7,8}$)/cell phone and telephone

If you use ValidForm to find the Validform_v5.3.2.js file, find the following code:

  code is as follows copy code

datatype:{
& nbsp;   "*":/[\w\w]+/,
    "*6-16":/^[\w\w]{6,16}$/,
    "n":/^\d+$ /,
    "n6-16":/^\d{6,16}$/,
    "s":/^[\u4e00-\u9fa5\uf900-\ufa2d\w\.\s]+$/ ,
    "s6-18":/^[\u4e00-\u9fa5\uf900-\ufa2d\w\.\s]{6,18}$/,
    "P":/^[0-9]{ 6}$/,
    "M":/^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$/,
    "E":/^ \w+ ([-+. '] \w+) *@\w+ ([-.] \w+) *\.\w+ ([-.] \w+) *$/,
    URL:/^ (\w+:\/\/) \w+ (\.\w+) +.*$/
},

Add the above positive to the back of it!

Or you can judge for yourself by the following methods:

  code is as follows copy code

 var reg = new RegExp (^13[0-9]{9}$|14[0-9]{9}|15[0-9]{9}$|18[0-9]{9}$) | ( ^0 (10|2[0-5789]|\\d{3}) \\d{7,8}$) ");
 if (!reg.test (Phone_mob)) {
        hialert (' Please enter the correct phone number ', ' Please note ', function () {
        $ ("#phone_mob"). focus ();
        });
               return false;
            }

The above code I use the Hialert plug-in, you can also directly with alert, hint!

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.