JS Regular expression Validation mailbox code

Source: Internet
Author: User

The following is a regular expression that validates an e-mail message

The code is as follows Copy Code

var re =/^w+ ([.-]?w+) *@w+ ([.-]?w+) * (. w{2,3}) +$/;

Cases

The code is as follows Copy Code

<pre class=html name= "code" ><div>
Please enter a valid mailbox: <br/>
<input type= "text" size=18 id= "email" onfocus= "clearform ();"/>
<input type= "button" onclick= "Validate ();" value= "Authentication"/>
</div>
<div id= "E-result" style= "Display:none" >
The mailbox you entered is valid. Thank you!
</div></PRE><BR>

<script language= "JavaScript1.2" >
function Vemail () {
var str=document.getelementbyid (' email '). Value;
if (Str.length = = 0) {
Alert ("Mailbox cannot be empty");
return false;
}
var rule=/^ ([W-]+ (?:.) [ w-]+) @ ((?: [w-]+.) *W[W-]{0,66}). ([a-z]{2,6} (?:. [ A-z]{2})? $/i;
if (Rule.test (str))
return true;
else{
Alert ("Please enter a valid mailbox!");
return false;
}
}
function Validate () {
if (document.layers| | document.getelementbyid| | document.all) {
if (Vemail ()) {
document.getElementById (' E-result '). style.display= "Block";
}
}
}
function ClearForm () {
document.getElementById (' E-result '). style.display= "None";
document.getElementById (' email '). value= "";
}
</script>

Now we're going to start dissecting this regular expression.

Re is a variable that stores the right regular expression, and in JavaScript, declares the variable to use the var keyword.

The reading order of regular expressions is Left-to-right

Regular expressions always start with (/) and end, and all content between slashes is part of a regular expression.

The caret (^) indicates that we want to use this expression to examine a string that begins with a particular string. If you remove the caret, the e-mail address may be considered valid even if there is a heap of junk characters at the beginning of the string.

An expression w represents any single character, including A~z, A~z, 0~9, or underscores. E-mail must start with these words Fu Zhi.

Plus + means we're looking for one or more occurrences of the previous entry.

The brackets () represent a group, which means that all of the contents of the parentheses are referenced later, so they are now placed in a group.

The bracket [] is used to indicate that any one of these characters can appear. In this example, the square brackets contain the characters.-. We want to allow the user to enter the dot or hyphen, but the point number is special to the regular expression, so you need to precede it with a backslash, and a backslash in front of the special character to denote the word escape, and the escaped character to represent its meaning. Because of parentheses, the input string can have a dot or a hyphen in this position, but both cannot exist at the same time.

Question mark? Indicates that the preceding entry can appear once or not. So the first part of the e-mail address can have a dot or a hyphen, or it may not.

In? Later, use w+ again to indicate that there must be other characters after the dot or hyphen.

The * number that appears after () indicates that the preceding entry can appear 0 or more times. So the contents of the parentheses can appear 0 or more times.

The @ character represents itself, without any other meaning, which is between the e-mail address and the domain name.

The @ character appears again after the w+, indicating that the character must appear after @. After that, it appears again ([.-]?w+) *, which indicates that the dot number or hyphen is allowed in the suffix of the e-mail address.

Then, create another group (. w{2,3}) in a pair of parentheses, indicating that we want to find a point number followed by some characters. In this example, the number in the curly braces indicates that the preceding entry can appear between 2 and 3 times. After this group is a + number, which means that the previous entry (this group) must appear one or more times. This will match. com or. edu, and also match ox.ac.uk.

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.