Re: email address syntax checker
| From: | Nilesh Govindarajan | Date: | Thu, 20 Jan 2011 13:33:22 +0000 |
| Subject: | Re: email address syntax checker | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-310896@lists.php.net to get a copy of this message | ||
On 01/20/2011 09:44 AM, Donovan Brooke wrote:
Hi Guys, I'm waddling my way through database interaction and thought someone on the list may already have a simple email checker that they'd like to share... you know, looking for the @ char and dots etc.. I did a quick search of the archives and found a couple elaborate things.. but I'm looking for something simple. This job will have trusted users and the checker is more to help them catch mistakes when registering. Thanks!, DonovanWell, I had created an email validator long ago, after a neat research on Google, reading RFCs, etc. I don't guarantee that it's without bugs, but it has been correct for me in all valid & invalid email addresses I used for test. Code: <?php function checkMail($mail) { if(strlen($mail) <= 0) {
return false;} $split = explode('@', $mail); if(count($split) > 2) {
return false;} list($username, $domain) = $split; /* * Don't allow * Two dots, Two @ * !, #, $, ^, &, *, (, ), [, ], {, }, ?, /, \, ~, `, <, >, ', " */ $userNameRegex1 = '/\.{2,}|@{2,}|[\!#\$\^&\*\(\)\[\]{}\?\/\\\|~`<>\'"]+/'; /* * Username should consist of only * A-Z, a-z, 0-9, -, ., _, +, % */ $userNameRegex2 = '/[a-z0-9_.+%-]+/i'; /* * Domain cannot contain two successive dots */ $domainRegex1 = '/\.{2,}/'; /* * Domain can contain only * A-Z, a-z, 0-9, ., -, */ $domainRegex2 = '/[a-z0-9.-]+/i'; if(preg_match($userNameRegex1, $username) or
!preg_match($userNameRegex2, $username) or
preg_match($domainRegex1, $domain) or
!preg_match($domainRegex2, $domain) or
!checkdnsrr($domain, 'MX')) {
return false;
} else {
return true;} } -- Regards, Nilesh Govindarajan Facebook: http://www.facebook.com/nilesh.gr Twitter: http://twitter.com/nileshgr Website: http://www.itech7.com