Verify Email accuracy using PHP

Source: Internet
Author: User
Please contact me with any Title: http://www.webjx.comweb @ webjx.com reprinted please indicate the source when you registered on a forum, usually have an e-mail address verification function, when you enter an illegal pattern will present some kind of error prompt letter what title please contact me: http://www.webjx.com web@webjx.com
Reprinted please indicate the source

When you register on a forum, there is usually an e-mail address verification function. when you enter an illegal pattern, an error message is displayed.

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

However, the above sub-statement only checks strings and does not support output. We can further use this formula to return 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@webjx.com) = FALSE)
{
Echo 'The input E_mail is inaccurate .';
}
Else
{
Echo 'input E_mail is accurate .';
}

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.