Verify Email correctness using PHP

Source: Internet
Author: User

When you register on a forum, there is usually an e-mail address verification function. When you enter an invalid format, an error message will appear.

We can use the following rule expression
Ereg ("^ [a-zA-Z0-9 _] @ [a-zA-Z0-9-]. [a-zA-Z0-9-.] $]", $ email );

However, the sub-function above only checks strings and does not support 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;
}

Next we can further check whether the host name exists:
List ($ Username, $ Domain) = split ("@", $ email );
If (getmxrr ($ Domain, $ MXHost ))
{
Return TRUE;
}
Else
{
If (fsockopen ($ Domain, 25, $ errno, $ errstr, 30 ))
{
Return TRUE;
}
Else
{
Return FALSE;
}
}

Now Let's organize the above two functions with 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, 25, $ errno, $ errstr, 30 ))
{
Return TRUE;
}
Else
{
Return FALSE;
}
}
}

Then we can use this function to check whether an input Email exists. For example:

If (checkEmail (web@etoow.com) = FALSE)
{
Echo "The E_mail you entered is incorrect .";
}
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.