/    Sign up×
Community /Pin to ProfileBookmark

validate an email

Is it possible to really validate an email address. I have tried REGEX and although it does weed out some things plenty of errors still get through. I have also tried the ‘enter email’ and ‘confirm email’ model as well but the problem is the same and it would seem people just copy and paste the errornous email from one box to another.

Obviously I cant check the email actually belongs to the user at the time of form submission but is there any way to check an email address is functional?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@BeachSideJul 13.2005 — Ah that is probably because you haven't tried this regex by Jeff Mott...

http://www.webdeveloper.com/forum/showthread.php?threadid=9970

I have yet to have that one fail
Copy linkTweet thisAlerts:
@bokehJul 13.2005 — Wow! That REGEX is frightening. The truth is nothing is conclusive. A similar discussion the other day [URL=http://myhomewebserver.co.uk/validate_email_with_the_xml_http_request_object]inspired me to write this[/URL] which actually connects to the remote mail server and checks the address while the client continues filling in the form.
Copy linkTweet thisAlerts:
@pratik_learnerJul 13.2005 — I have written the following class that returns 0 is the e-mail is valid or a number from 1-5 depending on the type of invalidation. Try using some network functions to check if the domain or the email exists but it is not reliable because it is subject to the uptime of the server. This silly cause could send a potential visitor ( or a customer) from signing up ( or purchasing ) for a service.

The better thing to do is to send the an autoresponder and check for the confirmation. The reason for this is someone might signup ( intentionally or otherwise) under someone else's email ID and the rightful entity to whom the ID belongs may accuse you of sending spam.

[code=php]<?PHP
/*
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
class ValidateEmail
{
function InvChrDM($em)
{
$flag = true;

for( $i = 0; $i < strlen($em); $i++)
{
$cLet = substr($em, $i, 1);
if( ctype_alnum($cLet) or $cLet == '-' or $cLet == '.' )
{
$flag = TRUE;
}
else
{
$flag = FALSE;
break;
}
}
return $flag;

}

function InvChrEM($em)
{
$flag = true;

for( $i = 0; $i < strlen($em); $i++)
{
$cLet = substr($em, $i, 1);
if( ctype_alnum($cLet) or $cLet == '_' or $cLet == '-' or $cLet == '.' )
{
$flag = TRUE;
}
else
{
$flag = FALSE;
break;
}
}
return $flag;

}

/* Checks for blank fields that contain only whitespace or nothing*/
function ChkBlnk($em)
{
/*Checks for ONLY whitespace fields
ctype_graph checks for printable characters
ctype_space chcks for whitespace like space tab etc.*/
if(!ctype_graph($em) and ctype_space($em))
{
return false;
}
/*Checks for blank fields*/
else if($em == "")
{
return false;
}
else
{
return true;
}
}


/* The following function checks for the @ character in the*/
function ChkEm($em)
{
if(strpbrk($em, '@'))
{
if(substr_count($em, '@') > 1)
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}


/*Checks for validity of domain name*/
function ChkDom($em)
{
/* Three character domain names */
$dm1 = array(
'.ac', '.ad', '.ae', '.af', '.ag', '.ai', '.al', '.am',
'.an', '.ao', '.aq', '.ar', '.as', '.at', '.au', '.aw',
'.az', '.ba', '.bb', '.bd', '.be', '.bf', '.bg', '.bh',
'.bi', '.bj', '.bm', '.bn', '.bo', '.br', '.bs', '.bt',
'.bv', '.bw', '.by', '.bz', '.ca', '.cc', '.cd', '.cf',
'.cg', '.ch', '.ci', '.ck', '.cl', '.cm', '.cn', '.co',
'.cr', '.cu', '.cv', '.cx', '.cy', '.cz', '.de', '.dj',
'.dk', '.dm', '.do', '.dz', '.ec', '.ee', '.eg', '.eh',
'.er', '.es', '.et', '.fi', '.fj', '.fk', '.fm', '.fo',
'.fr', '.ga', '.gd', '.ge', '.gf', '.gg', '.gh', '.gi',
'.gl', '.gm', '.gn', '.gp', '.gq', '.gr', '.gs', '.gt',
'.gu', '.gw', '.gy', '.hk', '.hm', '.hn', '.hr', '.ht',
'.hu', '.id', '.ie', '.il', '.im', '.in', '.io', '.iq',
'.ir', '.is', '.it', '.je', '.jm', '.jo', '.jp', '.ke',
'.kg', '.kh', '.ki', '.km', '.kn', '.kp', '.kr', '.kw',
'.ky', '.kz', '.la', '.lb', '.lc', '.li', '.lk', '.lr',
'.ls', '.lt', '.lu', '.lv', '.ly', '.ma', '.mc', '.md',
'.mg', '.mh', '.mk', '.ml', '.mm', '.mn', '.mo', '.mp',
'.mq', '.mr', '.ms', '.mt', '.mu', '.mv', '.mw', '.mx',
'.my', '.mz', '.na', '.nc', '.ne', '.nf', '.ng', '.ni',
'.nl', '.no', '.np', '.nr', '.nu', '.nz', '.om', '.pa',
'.pe', '.pf', '.pg', '.ph', '.pk', '.pl', '.pm', '.pn',
'.pr', '.ps', '.pt', '.pw', '.py', '.qa', '.re', '.ro',
'.ru', '.rw', '.sa', '.sb', '.sc', '.sd', '.se', '.sg',
'.sh', '.si', '.sj', '.sk', '.sl', '.sm', '.sn', '.so',
'.sr', '.st', '.sv', '.sy', '.sz', '.tc', '.td', '.tf',
'.tg', '.th', '.tj', '.tk', '.tm', '.tn', '.to', '.tp',
'.tr', '.tt', '.tv', '.tw', '.tz', '.ua', '.ug', '.uk',
'.um', '.us', '.uy', '.uz', '.va', '.vc', '.ve', '.vg',
'.vi', '.vn', '.vu', '.wf', '.ws', '.ye', '.yt', '.yu',
'.za', '.zm', '.zw'
);

/* Four character domain names */
$dm2 = array(
'.biz','.com','.edu','.gov','.int','.mil','.net', 'org',
'.pro'
);
/*Five character domain names */
$dm3 = array('aero','.coop','.info','.name');

/* Solitary seven-letter domain*/
$dm7 = '.museum';
/*Finds string length and substrings */
$len = strlen($em);
$sub3 = substr($em,$len-3, $len);
$sub4 = substr($em,$len-4, $len);
$sub5 = substr($em,$len-5, $len);
$sub7 = substr($em,$len-7, $len);

/*Searches if the any of substrings match the listed domain
suffixes*/

$flag = false;
/* Searches if the three letter substring is valid*/
if( $flag != true )
{
for($i = 0; $i < count($dm1); $i++)
{
if($sub3 == $dm1[$i])
{
return true;
$flag = true;
break;
}
}
}
/* Searches if the four letter substring is valid*/
if( $flag != true )
{
for($j = 0; $j < count($dm2); $j++)
{

if($sub4 == $dm2[$j])
{
return true;
$flag = true;
break;
}
}
}
/* Searches if the five letter substring is valid*/

if( $flag != true )
{
for($k = 0; $k < count($dm3); $k++)
{
if( $sub5 == $dm3[$k])
{
return true;
$flag = true;
break;
}
}
}
/* Searches for seven letter .museum domain*/
if( $flag != true )
{
for($l = 0; $l < count($dm7); $l++)
{
if( $sub7 == $dm7)
{
return true;
$flag = true;
break;
}
}
}
/* Return false if none of substrings match the listed domain names*/
if( $flag == false)
{
return false;
}


}
/*Returns the string that comes after the @ character*/
function SpltEmID($em,$what)
{
$dmn = strpbrk($em, '@');
$dmn = explode('@',$dmn);
if($what == 'domain')
{
return $dmn[0];
}
else if($what == 'email')
{
return $dmn[1];
}
else
{
return FALSE;
}
}

};


function IsValid($eml)
{
$dm = ValidateEmail::SpltEmID($eml,'email');
$em = ValidateEmail::SpltEmID($eml,'domain');

$tmp1 = ValidateEmail::ChkBlnk($eml);
$tmp2 = ValidateEmail::ChkEm($eml);

$tmp3 = ValidateEmail::ChkDom($dm);
$tmp4 = ValidateEmail::InvChrDM($dm);
$tmp5 = ValidateEmail::InvChrEM($em);

while(1)
{
if(!$tmp1)
{
return '1';
break;
}
elseif(!$tmp2)
{
return '2';
break;
}
elseif(!$tmp3)
{
return '3';
break;
}
elseif(!$tmp4)
{
return '4';
break;
}
elseif(!$tmp5)
{
return '5';
break;
}
else
{
return '0';
break;
}
}
/*The function returns the following integers for invalid
error types
* 1 if blank field
* 2 if @ char not found
* 3 if email on invalid TLD
* 4 if invalid characters are found in domain name
* 5 if invalid characters are found in email

* 0 if the email is valid

NOTE : the values returned are in strings and are
*NOT* Boolean

*/
}

/*Zip Files Classes*/
/* http://www.phpclasses.org/browse/package/945.html */
/* Send HTML pages */
/* http://www.phpguru.org/static/mime.mail.html */
/* http://phpmailer.sourceforge.net */

/* SQL Courses */
/* http://www.sqlcourse.com/ */
/* http://www.w3schools.com/sql/ */
/* http://www.oreillynet.com/pub/ct/19 */

?>
[/code]
Copy linkTweet thisAlerts:
@BeachSideJul 13.2005 — A similar discussion the other day [URL=http://myhomewebserver.co.uk/validate_email_with_the_xml_http_request_object]inspired me to write this[/URL] which actually connects to the remote mail server and checks the address while the client continues filling in the form.[/QUOTE]

Dude! That is damn sexy! ?

Any chance in getting a look at that script?
Copy linkTweet thisAlerts:
@Brain_StormauthorJul 13.2005 — A similar discussion the other day [URL=http://myhomewebserver.co.uk/validate_email_with_the_xml_http_request_object]inspired me to write this[/URL] which actually connects to the remote mail server and checks the address while the client continues filling in the form.[/QUOTE]
I like that. How does it work?
Copy linkTweet thisAlerts:
@bokehJul 13.2005 — I like that. How does it work?[/QUOTE]Did you want the code? Even checking to this degree still gives some false positives because some mailservers accept everything they have authority for and just bin it later. But if the remote mailserver refuses the address obviously it is unusable.
Copy linkTweet thisAlerts:
@BeachSideJul 14.2005 — Yeah but if it is refused you can make a sort of back-up that would allow a person to still send the form except with a "flag" so maybe if they were signing up for a service it would be pending instead of automatically signing them up.

So you could use that as the first line of defense I guess. Still man that is freakin killer. I dig it for sure.
×

Success!

Help @Brain_Storm spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...