JS Email e-mail address verification Program

Source: Internet
Author: User
Tags chr

The original code is like this:

The code is as follows Copy Code

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

Seems

This verifies that mailboxes like sofish@163.com are fine. However, with a closer look at the non-capture reference after at (@), you are using * (any occurrence):

The code is as follows Copy Code

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:

The code is as follows Copy Code
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). And, what should be noted here is 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:

The code is as follows Copy Code

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

Instance

The code is as follows Copy Code

<script>
function Validate ()
{
var pattern=/^w+ ([-+.] w+) *@w+ ([-.] w+) *.w+ ([-.] w+) *$/;
var email = document.getElementById ("Mail"). Value;

if (!pattern.test (email))
Alert ("wrong");
Else
Alert (' Right ')
}
</script>

<input type= "text" name= "Mail" id= "Mail" >
<input type= "button" value= "onclick=" Validate () ">

Name= "Mail" id= "Mail" ><input type= "button" value= "" onclick= "Validate ()" >

JavaScript Verify mailbox format

The code is as follows Copy Code
<script Language=javascript runat=server>
function Isemail (stremail) {
if (/^w+ (-w+) | (stremail.search) | (. w+)) *@[a-za-z0-9]+ ((. | -) [a-za-z0-9]+] *. [a-za-z0-9]+$/)!=-1)
return true;
Else
Alert ("Oh");
}
</SCRIPT>
<input type=text onblur=isemail (this.value) >

Second: Use JavaScript to verify that email is correctly filled in

  code is as follows copy code

<title>test</title>
< Script language= "javascript"
function Emailcheck () {
var emailstr=document.all.form1.tel.value;
Alert (EMAILSTR);
var emailpat=/^ (. +) @ (. +) $/;
var matcharray=emailstr.match (Emailpat);
if (matcharray==null) {
Alert ("E-mail address must include (@ and.)")
return false;
}
return true;
}
</script>

<body>
<form name= "Form1"
<input type= "text" Name= "Tel"/>
<input type= "button" value= "Press" onclick= "Emailcheck ()"/>
</form>


</span>
</body>

The third type:

The code is as follows Copy Code

JS Verification Email
function Char_test (CHR)
Character detection function
{
var i;
var smallch= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var bigch= "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (i=0;i<26;i++)
if (Chr==smallch.charat (i) | | Chr==bigch.charat (i))
return (1);
return (0);
}

function Spchar_test (CHR)
Digital and special character detection functions
{
var i;
var spch= "_-.0123456789";
for (i=0;i<13;i++)
if (Chr==spch.charat (i))
return (1);
return (0);
}

function Email_test (str)
{
var i,flag=0;
var at_symbol=0;
Location of "@" detected
var dot_symbol=0;
“.” Location of the test
if (Char_test (Str.charat (0)) ==0)
return (1);
First character must be in letter

for (i=1;i<str.length;i++)
if (Str.charat (i) = = ' @ ')
{
At_symbol=i;
Break
}
Detect the location of "@"

if (at_symbol==str.length-1 | | at_symbol==0)
return (2);
No mail server domain name

if (at_symbol<3)
return (3);
Account number is less than three characters

if (at_symbol>19)
return (4);
Account number is more than 19 characters

for (i=1;i<at_symbol;i++)
if (Char_test (Str.charat (i)) ==0 && spchar_test (Str.charat (i)) ==0)
return (5);
for (i=at_symbol+1;i<str.length;i++)
if (Char_test (Str.charat (i)) ==0 && spchar_test (Str.charat (i)) ==0)
return (5);
Cannot use other special characters

for (i=at_symbol+1;i<str.length;i++)
if (Str.charat (i) = = '. ') dot_symbol=i;
for (i=at_symbol+1;i<str.length;i++)
if (dot_symbol==0 | | dot_symbol==str.length-1)
Simple detection has no "." To determine whether the server name is legitimate
return (6);

return (0);
Mail name Legal
}

Add to the Knowledge

1. /^$/This is a generic format.
         ^ matches the start position of the input string; $ matches the end position of the input string
 2. Enter the function you want to implement.
        * matches the preceding subexpression 0 or more times;
       + Matches the preceding subexpression one or more times;
       Matches the preceding subexpression 0 times or once;
       d  matches a numeric character, equivalent to [0-9]

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.