Use PHP to verify that the email is correct

Source: Internet
Author: User
Tags split domain
If you have any questions please contact me: http://www.webjx.com web@webjx.com
Reprint please indicate the source

When you register on a forum, there is usually an e-mail address verification function, when you enter an illegal format, there will be some kind of error message.

We can use the following rule expression
Ereg ("^[a-za-z0-9_]+@[a-za-z0-9\-]+\.[ A-za-z0-9\-\.] +$] ", $email);

But the function of the above equation is to check only strings and not output. We can further use this formula to achieve the function of returning information:
if (eregi ("^[a-za-z0-9_]+@[a-za-z0-9\-]+\.[ A-za-z0-9\-\.] +$] ", $email)
{
return FALSE;
}

Here we can go further to detect the host name, does it exist:
List ($username, $domain) = Split ("@", $email);
if (GETMXRR ($domain, $mxhost))
{
return TRUE;
}
Else
{
if (Fsockopen ($domain, $errno, $errstr, 30))
{
return TRUE;
}
Else
{
return FALSE;
}
}

Now we'll organize the top two features in PHP to form a function:
function Checkemail ($email)

{if (eregi ("^[a-za-z0-9_]+@[a-za-z0-9\-]+\.[ A-za-z0-9\-\.] +$] ", $email)
{
return FALSE;
}

List ($username, $domain) = Split ("@", $email);

if (GETMXRR ($domain, $mxhost))
{
return TRUE;
}
Else
{
if (Fsockopen ($domain, $errno, $errstr, 30))
{
return TRUE;
}
Else
{
return FALSE;
}
}
}

We can then use this function to detect the existence of an incoming email, for example:

if (Checkemail (web@webjx.com) = = FALSE)
{
echo "The E_Mail you entered is not correct.";
}
Else
{
echo "The input e_mail is correct."
}


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.