PHP Verify mailbox and mailbox validation

Source: Internet
Author: User
Tags character set ereg explode regular expression to domain

A

Domain name is any combination of a specific character set, English letters, numbers, and "-" (that is, hyphens or minus signs) in each country's text, but neither the beginning nor the end can contain a "-", "-" that does not appear consecutively. The letters in the domain name are not case-insensitive. The domain name can be up to 60 bytes (including suffix. com,. NET,. org, and so on).
/^[a-z] ([a-z0-9]*[-_]?[ a-z0-9]+) *@ ([a-z0-9]*[-_]?[ a-z0-9]+) +[.] [A-z] {2,3} ([.] [A-z] {2})? $/i;
/content/I constitutes a case-insensitive regular expression

The code is as follows Copy Code

$email = "tanklo_--vehy@yahoo.com.cn";
function Check_email ($email) {
$pattern _test = "/([A-z0-9]*[-_.]? [a-z0-9]+) *@ ([a-z0-9]*[-_]?[ a-z0-9]+) +[.] [A-z] {2,3} ([.] [A-z] {2})? /I ";
Return Preg_match ($pattern _test, $email);
}
echo Check_email ($email);

The above can only be simple to verify the message format, the following a more specific verification method.

How PHP Validates:

The code is as follows Copy Code

?

PHP validation of mailbox format functions
function Valid_email ($email) {
We check that there ' s one @ symbol, and that's the lengths are right
if (!ereg ("^[^@]{1,64}@[^@]{1,255}$", $email)) {
Email invalid because wrong number of characters in one section, or wrong number of @ symbols.
return false;
}
Split it into sections to make life easier
$email _array = explode ("@", $email);
$local _array = Explode (".", $email _array[0]);
for ($i = 0; $i < sizeof ($local _array); $i + +) {
if (!ereg ([a-za-z0-9!#$%& #038; ' *+/=?^_`{|} ~-][a-za-z0-9!#$%& #038; ' *+/=?^_`{|} ~.-]{0,63}) | ("[^(\|")] {0,62} ")) $", $local _array[$i])) {
return false;
}
}
if (!ereg ("^[?[ 0-9.]+]?$ ", $email _array[1])) {//Check if domain is IP. If not, it should be valid domain name
$domain _array = Explode (".", $email _array[1]);
if (sizeof ($domain _array) < 2) {
return false; Not enough parts to domain
}
for ($i = 0; $i < sizeof ($domain _array); $i + +) {
if (!ereg ("^" ([a-za-z0-9][a-za-z0-9-]{0,61}[a-za-z0-9]) | ( [a-za-z0-9]+)) $ ", $domain _array[$i])) {
return false;
}
}
}
return true;
}


application Example

if (!valid_email ($myeml)) {
echo "<script>alert (' please enter a valid e-mail address '); location.href= ' Javascript:history.back () ' </ Script> ";
Break
}
?>

Accidentally found the CHECKDNSRR function where there is more exquisite examples, to share to everyone!

  code is as follows copy code

< php

Function Validate_email ($email) {

$exp = ' ^[a-z ' 0-9]+ ([. _-][a-z ' 0-9]+) *@ ([a-z0-9]+ (. _-][a-z0-9]+)) +$ ";

If (eregi ($exp, $email)) {//Validate the validity of the email format first with a regular expression

if (CHECKDNSRR (Array_pop (Explode ("@", $email)), "MX") {// Then use CHECKDNSRR to verify the validity of the domain name portion of the email
return true;
} else{
return false;
}

}else{

return false;

}

??

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.