Jump to content

Email Validation


br0ken

Recommended Posts

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

  • 7 months later...

 

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.