Use a Javascript Regular Expression to verify the Email address

Source: Internet
Author: User
It seems that it is okay to verify a mailbox like sofish@163.com. However, for a closer look AT the non-capturing reference behind AT (@), the usage is * (any occurrence)

First look at the following code:

FunctionisValidMail (sText) {varreMail =/^ (? : \ W + \.?) * \ W + @(? : \ W + \.?) * \ W + $/; returnreMail. test (sText );}

It seems that it is okay to verify a mailbox like sofish@163.com. However, for a closer look AT the non-capturing reference behind AT (@), the usage is * (any occurrence ):

VarreMail =/^ (? : \ W + \.?) * \ W + @(? : \ W + \.?) * \ W + $ /;

This also includes 0 occurrences. A mailbox like sofish @ 163com is also verified. Obviously, "." must appear at least once. Therefore, "+" indicates at least once. But here, after AT, we can write an end like 163.com.cn, but directly change it to "+", so that 163..com.cn can pass verification. The following is my method:

FunctionisValidMail (sText) {varreMail =/^ (? : \ W + \.?) * \ W + @(? : \ W + \.) + \ w + $/; alert (reMail. test (sText ))}

The rule "." appears only once. Then, after a non-capturing reference, make the rest display at least once and end with any character. However, here the "\ w" is underlined, that is to say, mail like sofish@163_.com _ may also be difficult to pass, apparently, this is an invalid email, ". you can only use English letters (at least I have never seen a domain name followed by a number ). In addition, it should be noted that "\ w" represents the characters including underscores, and does not need to be abbreviated as follows:

A-zA-Z_0-9

Therefore, the code above can be modified as follows:

FunctionisValidMail (sText) {varreMail =/^ (? : [A-z \ d] + [_ \-\ + \.]?) * [A-z \ d] + @(? :( [A-z \ d] + \-?) * [A-z \ d] + \.) + ([a-z] {2,}) + $/I; alert (reMail. test (sText ))}

In fact, you can write it like this, right. To be more thorough, you can also consider not using the same letter after each point AT, such as .com.cn.com.cn (obviously, such domain names still exist, and I am wrong, thanks to @ StonyWang for reminding me, is there anyone doing this ?) This is not allowed. You can consider using capture references for storage and comparison and verification. Think of it as an exercise question (it suddenly seems like a math question in high school ).

The instance code is as follows:

Test: Use a Javascript Regular Expression to verify the Email address Enter the Email address and click test. The error address will return false... correct, displayed? Guess by yourself:

Var sText = document. getElementById ('mail '). value; document. getElementById ('mail '). onblur = function sValue () {sText = this. value;} function isValidMail (sText) {var reMail =/^ (? : [A-zA-Z0-9] + [_ \-\ + \.]?) * [A-zA-Z0-9] + @(? :( [A-zA-Z0-9] + [_ \-]?) * [A-zA-Z0-9] + \.) + ([a-zA-Z] {2,}) + $/; alert (reMail. test (sText)} script
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.