br0ken Posted June 28, 2008 Share Posted June 28, 2008 Every single email validation function I've seen uses some form of regex, however today I found out about PHP filters. I was wondering if there is any downsides to using PHP filters? The reason I ask this because in my opinion, it seems much simpler to use these rather than a complex regex operation. Here is my function. function validEmail($email) { if (filter_var($email, FILTER_VALIDATE_EMAIL)) { return true; } else { return false; } } To me the above seems much simpler yet I see no one using it! Link to comment https://forums.phpfreaks.com/topic/112332-email-validation/ Share on other sites More sharing options...
shadiadiph Posted February 22, 2009 Share Posted February 22, 2009 doesn't work properly try it with [email protected] Link to comment https://forums.phpfreaks.com/topic/112332-email-validation/#findComment-768316 Share on other sites More sharing options...
br0ken Posted February 22, 2009 Author Share Posted February 22, 2009 Really? That's odd. I can't test it right now but that's an actual PHP function, it shouldn't have any errors. Can anyone else confirm this? Link to comment https://forums.phpfreaks.com/topic/112332-email-validation/#findComment-768347 Share on other sites More sharing options...
shadiadiph Posted February 22, 2009 Share Posted February 22, 2009 It shouldn't you are right but it allowed me to submit what i typed with no errors I have found one that seems to work but i am sure it has some error somewhere Link to comment https://forums.phpfreaks.com/topic/112332-email-validation/#findComment-768355 Share on other sites More sharing options...
redarrow Posted February 22, 2009 Share Posted February 22, 2009 Little example for you m8. If you need to no what the regexp is doing just ask. <?php $email=array("me@me_.com","_me@m_e.net","[email protected]"); $e=implode(' ',$email); if(preg_match("/[a-z0-9\-\_]{0,50}+@[a-z0-9\_\-]{0,50}\.[a-z]{0,3}$/i",$e)){ echo " correct emails\n $e "; }else { echo "incorrect emails\n $e"; } ?> Link to comment https://forums.phpfreaks.com/topic/112332-email-validation/#findComment-768381 Share on other sites More sharing options...
br0ken Posted February 22, 2009 Author Share Posted February 22, 2009 doesn't work properly try it with [email protected] Isn't that a valid email though? Domains can contain hyphens so 2abc-.com is no different from something like php-freaks.com. Link to comment https://forums.phpfreaks.com/topic/112332-email-validation/#findComment-768465 Share on other sites More sharing options...
shadiadiph Posted February 22, 2009 Share Posted February 22, 2009 the closest I have found that works so far is but i am sure it has an error somewhere but i haven't found it yet. $emailx ="/^[a-z0-9]+([_.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\\.[a-z]{2,4}$/"; if ($email=="") { $error[] ="Email is a required field please complete and submit it again."; } elseif (preg_match($emailx, $email) ==false) { $error[] ="Please fill in a correct email address"; } Link to comment https://forums.phpfreaks.com/topic/112332-email-validation/#findComment-768470 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.