Code to validate messages through JS regular expressions

Source: Internet
Author: User
Tags end expression mail mail code reference regular expression domain

Introduction: In JavaScript to verify the mail code is very common, the Internet can find many, mainly through the regular expression of JS to achieve, this article mainly discusses this very common code, suitable for novice reading learning

Daytime always play not to read, these two nights is very magical because can't sleep up to read, regular expression. It's a funny thing. "Javascript Advanced Programming" is a good book, written very systematically. However, in the regular expression that chapter, see the verification e-mail there, it seems that the formal expression to write wrong, I was accidentally found.

The original code is like this:

function Isvalidmail (stext) {
var reMail =/^ (?: \ W+\.?) *\w+@ (?: \ W+\.?) *\w+$/;
Return Remail.test (stext);
}

It seems that it is no problem to verify a mailbox like sofish@163.com. However, with a closer look at the non-capture reference after at (@), you are using * (any occurrence):

var reMail =/^ (?: \ W+\.?) *\w+@ (?: \ W+\.?) *\w+$/;

Well, it also includes 0 times, so to say. Mailboxes such as sofish@163com are also validated. Obviously, "." Must occur at least once, and therefore "+" does not represent at least one occurrence. But here, at the back of the AT, we can write the end like 163.com.cn, but directly to "+", so that 163..com.cn can also pass the verification. Here is my method:

function Isvalidmail (stext) {
var reMail =/^ (?: \ W+\.?) *\w+@ (?: \ W+\.) +\w+$/;
Alert (Remail.test (stext))
}

Rules "." Number appears only once. Then, after the non capture reference, let the other display at least 1 times, and then end with any character. However, the "\w" here is underlined, that is to say, mail like Sofish@163_.com_ is also likely to pass through difficult, apparently, this is an illegal message, at the back of the "." Before, is not to be underlined, and the back, can only be English letters (at least for now I have not seen with the following is a number of domain names). Also, it should be noted here that the characters represented by "\w" include underscores, without abbreviations:

[A-za-z_0-9]

Therefore, the above code can be modified in this way:

function Isvalidmail (stext) {
var reMail =/^ (?: [A-z\d]+[_\-\+\.]?) *[a-z\d]+@ (?:( [a-z\d]+\-?] *[a-z\d]+\.) + ([A-z]{2,}) +$/i;
Alert (Remail.test (stext))
}

Actually, that's usually what you write, right. To be more in-depth, you can also consider after the points at the end of the at, should not use the same letter, such as. com.cn.com.cn (obviously, such a domain or some, I was wrong, thank you @Stonywang Classmate's reminder, really someone so embarrassed to do so? This is not allowed to be asked. Consider using a capture reference for storage and for comparison validation. As an exercise bar (suddenly feel like a math problem in high school).



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.