Re: email address syntax checker

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Nilesh Govindarajan wrote:
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!,
Donovan



Well, 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;
}

}


Thanks! I think I'll go w/ Peter's suggestion for this site, but will
take note of this for reference's sake!

Cheers,
Donovan


--
D Brooke

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux