Jump to content

Validating Emails Function


ballouta

Recommended Posts

Hello

I have an option to upload a text file and read each line (which should be an email address)

the uploading is working fine, but I have problem with the validation function. in the same time i am not sure if the validation function is correct, I googled for several functions but all are giving me the same output that i will describe.

 

validation.php

function checkEmail($email){
return eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email);
}

 

I also tried this function:

function checkEmail($email) {
if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email))
return true;
else
return false;
}

 

 

while the process code is:

foreach($text as $line) {

//Convert to lowercase first	
$str = strtolower($line);

//Now Validate address
if (checkEmail($str))
	{
		//check if this email address already exsist
		$result = mysql_query("SELECT * FROM `mlist_01` where `email` = '$str' ");
		$count=mysql_num_rows($result);

		if($count== 1)
			{
		echo "<p dir='ltr'><font color='#FF0000'>This email address $str already exists</font></p>";
			}
		else
			{
		//$query="INSERT INTO `mlist_01` (`email` ) VALUES ('$str')";
		//$query_result = mysql_query ($query);
		echo "<p dir='ltr'><font color='#008000'>The email address $str has been inserted</font></p>";
			}


	}
else
	{
		echo "<p dir='ltr'><font color='#FF0000'>This email address $str is NOT valid</font></p>";
	}

}//for each loop

 

 

The output is strange:

 

Only the first line/email appears is green and says it has been inserted:

The email address [email protected] has been inserted

 

This email address [email protected] is NOT valid (real test)

 

where all the rest are in red and says invalid emails:

This email address [email protected] is NOT valid

This email address [email protected] is NOT valid

 

Please Help

Many Thanks

Link to comment
https://forums.phpfreaks.com/topic/190733-validating-emails-function/
Share on other sites

Firstly eregi is soon to be removed from PHP (it's deprecated as of version 5.3.0 and completely removed in PHP6). You should use it's PREG alternate functions, in this case preg_match

You could always use filter_var to check the email using the FILTER_VALIDATE_EMAIL filter to check it's a legitimate email pattern as well

Thanks for update info

 

the validation function is now:

 

function checkEmail($email)
{
if(!filter_var( $email, FILTER_VALIDATE_EMAIL))
return false;
else
return true;
}

 

But still the first email is validated as true and the rest are invalid.

 

Please help

Thank you

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.