PHP and JavaScript mailbox authentication implementation program

Source: Internet
Author: User
Tags ereg lowercase php and regular expression

The correct format for the message is: User name +@+ server name +. +com/net/cn/org
Note: 1, verify that the user name is the size of letters or lowercase letters
2, after the user name to add @
3, after the @ To add the name of the server.
4, after the service name to add "point" that is.
5, finally to COM net cn org


JavaScript Mailbox Authentication Code:

Example 1

The code is as follows Copy Code

var re=/^ ([a-za-z0-9]+[_|_|.]?) *[a-za-z0-9]+@ ([a-za-z0-9]+[_|_|.]?) *[a-za-z0-9]+. [A-za-z] {2,3}$/;
var e=form1.email.value;//gets the value of the email in the form Form1
if (E!=null && E.trim (). length<=0) {
if (!re.test (e))
{
Alert ("Sorry, the email you typed is illegal!");
}
}

String.prototype.trim=function () {return this.replace (^s*) | ( s*$)/g, ""); //Remove the null character, this function can remove the left and right sides of the null characters

Example 2

The code is as follows Copy Code

<script language= "Javascript1.2" >
<!--start hiding
function verifyaddress (obj)
{
var email = obj.email.value;
var pattern =/^ ([a-za-z0-9_-]) +@ ([a-za-z0-9_-]) + (. [ A-za-z0-9_-]) +/;
Flag = pattern.test (email);
if (flag)
{
Alert ("Your email address is correct!");
return true;
}
Else
{
Alert ("Please try again!");
return false;
}
}
Stop hiding-->
</script>
<body>
<form onsubmit= "Return to Verifyaddress (this);" >
<input name= "Email" type= "text"/>
<input type= "Submit"/>
</form>
</body>


PHP Mailbox Authentication Code:

The code is as follows Copy Code

function Checkemailaddr ($mailAddr) {return (!eregi ("^[_a-z0-9-]+") (. [ _a-z0-9-]+) *@[a-z0-9-]+ (. [ a-z0-9-]+) *$ ", $mailAddr)"? False:true;} function Checkemailaddr ($mailAddr) {

Return (!eregi ("^[_a-z0-9-]+") (. [ _a-z0-9-]+) *@[a-z0-9-]+ (. [ a-z0-9-]+) *$ ", $mailAddr)"? False:true;

}

PHP Instance 2

The code is as follows Copy Code

< PHP
if (Ereg ("^ [a-za-z0-9_-]) +@ ([a-za-z0-9_-]) + (. [ A-za-z0-9_-]) + ", $email))" {
echo "Your email address is correct!";}
else{
echo "Please try again!";
}
?>


Example 3

The code is as follows Copy Code

<?php
$email = "gdygdy_007@163.com";
if (Preg_match ("/[a-za-z0-9]+@[a-za-z0-9]+.[ a-z]{2,4}/", $email, $mail)) {
Echo ' yes<br> ';
}else{
Echo ' no<br> ';
}
echo "$email??". $email. " <> ";
echo "$mail??". $mail;
?>


Construct regular expressions to check email
There are three parts in a full email address:
1. Username (everything on the ' @ ' left)
2. ' @ '
3. Server name (that's the rest of the section)
User names can contain uppercase and lowercase Arabic numerals, periods ('. ') Minus sign ('-') and underscore ' _ '. The server name also complies with this rule, with the exception of the underscore.
Now, the start and end of a username cannot be a period, as is the case with the server. And you can't have two consecutive periods there's at least one character between them, okay now let's take a look at how to write a matching pattern for the user name:
^[_a-za-z0-9-]+$
It is not possible to allow a period to exist yet. We add it to:
^[_a-za-z0-9-]+ (. [ _a-za-z0-9-]+) *$
The above means: start with at least one canonical character (except.) followed by 0 or more strings starting with dots.
To simplify, we can use EREGI () instead of Ereg (), eregi () is not sensitive to case, we do not need to specify two range "A-Z" and "A-Z" only need to specify one on it:
^[_a-z0-9-]+ (. [ _a-z0-9-]+) *$
The following server name is the same, but to remove the underline:
^[a-z0-9-]+ (. [ a-z0-9-]+) *$
Good. Now just use the "@" to connect the two parts:
^[_a-z0-9-]+ (. [ _a-z0-9-]+) *@[a-z0-9-]+ (. [ a-z0-9-]+) *$
This is the complete email authentication matching mode, only need to call:
Eregi ("^[_a-z0-9-]+" (. [ _a-z0-9-]+) *@[a-z0-9-]+ (. [ a-z0-9-]+) *$ ", $eamil)
We can get an email.
Other uses of regular expressions
Extract string
Ereg () and eregi () has an attribute that allows the user to extract part of the string through a regular expression (you can read the manual). For example, we want to extract the filename from Path/url, and the following code is what you need:
Ereg ("([^\/]*) $", $PATHORURL, $regs);
echo $regs [1];
High-level substitution
Ereg_replace () and Eregi_replace () are also useful if we want to replace all the interval minus signs with commas:
Ereg_replace ("[nrt]+", ",", Trim ($STR));
Finally, I have another series of check email regular expression to let you look at the article analysis:
"^[-!#$%& ' *+\./0-9=?" A-z^_ ' a-z{|} ~]+'.' @'.' [-!#$%& ' *+\/0-9=? A-z^_ ' a-z{|} ~]+.'.' [-!#$%& ' *+\./0-9=? A-z^_ ' a-z{|} ~]+$

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.