4 Replies - 5063 Views - Last Post: 18 November 2014 - 12:24 PM

#1 Glorfindal  Icon User is offline

  • Java Enthusiast
  • member icon

Reputation: 37
  • View blog
  • Posts: 606
  • Joined: 31-December 08

PHP Function to validate email address

Post icon  Posted 25 October 2014 - 09:18 PM

This function checks if the structure of an email address is valid. In order to do this it looks for an @ symbol and splits the email address by the @ symbol and then searches for a period in the second portion. This function is heavily commented and designed so that it can be easily expanded. The function returns a string no matter what, if the email is valid is returns "true" and if isn't it returns a reason.

//Function to check emails
function validEmail($data) {
	//Store whether the email has been rejected
	$rejected = "";
	
	//Store the reason it was rejected
	$reason = "";
	
	//Check if the string contains a space
	if (strpos($data,' ') !== false) {
		$reason = "That is not a valid email.";
		$rejected = "true";
	}
	//Check if the string contains an at symbol
	if (strpos($data,'@') !== false) {
		$emailParts = explode("@", $data);
		if(sizeof($emailParts) == 2)
		{
			//Check if the second part of the email contains a period
			if(strPos($emailParts[1], ".") !== false) {
			} else {
				if(!$rejected == "true") {
					$reason = "That is not a valid email.";
					$rejected = "true";
				}
			}
		}
		else
		{
			if(!$rejected == "true") {
				$reason = "That is not a valid email.";
				$rejected = "true";
			}
		}
	}
	else
	{
		if(!$rejected == "true") {
			$reason = "That is not a valid email.";
			$rejected = "true";
		}
	}
	
	//Return the results
	if($rejected == "true") {
		return $reason;
	}
	else
	{
		return "true";
	}
}


Is This A Good Question/Topic? 1
  • +

Replies To: PHP Function to validate email address

#2 GetSet  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 411
  • Joined: 08-February 11

Re: PHP Function to validate email address

Posted 08 November 2014 - 08:54 PM

I also use strings as booleans on occasion (str == "" --> true). I did find however on one project some years ago that this proved problematic in terms of reusability. If I were to code the same now, I would implement either an exception or a separate error code variable returned by the function; the string could then be generated separate from the function based on the exception or error code. But all in all, for non-reusability issues, works great.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 5078
  • View blog
  • Posts: 13,707
  • Joined: 18-April 07

Re: PHP Function to validate email address

Posted 08 November 2014 - 09:34 PM

I am not quite sure why anyone would use this function for email validity. It simply checks if there is an at symbol and a period in the second piece. This doesn't mean it is valid. Go to the wikipedia page for Email address... http://en.wikipedia....email_addresses and look at the list of invalid email examples there. Run example bullet 3 until the end of the list. All those are invalid but in this function they would pass as valid.

Email address validation is pretty tricky and that is why people often use some pretty complex regular expressions for matching this. I don't expect people to go all the way to supporting RFC details in all their glory, but the above mentioned example isn't really all that complete or accurate and should probably not be used in most applications.

Besides that it is also pretty lengthy for a few checks. Could be tightened up a bit I think. :)
Was This Post Helpful? 0
  • +
  • -

#4 GetSet  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 411
  • Joined: 08-February 11

Re: PHP Function to validate email address

Posted 10 November 2014 - 12:03 AM

The email validity function would of course have to have a required target audience:
(1) Potential spammers or those who do not want their email address privy to the host
(2) Legitimate senders who simply made an error in their typing

If the case is for those class of people of (2), such a function by the OP is good enough.

In the case of those class of people of (1), the address is undeliverable in any event, and furthermore, as new email domains come about, the function of (2) doesn't run into the problem of being outdated.
Was This Post Helpful? 0
  • +
  • -

#5 Snijder  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 18-November 14

Re: PHP Function to validate email address

Posted 18 November 2014 - 12:24 PM

why not just use this?

if(filter_var($data, FILTER_VALIDATE_EMAIL)){
echo 'email is valid';
}else{
echo 'email is not valid';
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1