Solved

php email validation problem

Posted on 2008-10-20
4
147 Views
Last Modified: 2008-11-01
Hi,

I'm having a few problems validating email addresses using the following function: (see attched code snippet)

It won't allow email adresses with hyphens ... eg:

'matt.down@domain.com' is fine .. but... matt-stephen.down@domain.com' is rejected.

I'm no expert on regular expressions so any help would be really appreciated.


// check for a valid email address

function check_email_address($email) {

// First, we check that there's one @ symbol, and that the lengths are right

if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {

// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.

return false;

}

// Split it into sections to make life easier

$email_array = explode("@", $email);

$local_array = explode(".", $email_array[0]);

	for ($i = 0; $i < sizeof($local_array); $i++) {

		if(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {

		return false;

		}

	}

	if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name

	$domain_array = explode(".", $email_array[1]);

		if (sizeof($domain_array) < 2) {

			return false; // Not enough parts to domain

		}

		for ($i = 0; $i < sizeof($domain_array); $i++) {

			if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {

			return false;

			}

		}

	}

return true;

}

Open in new window

0
Comment
Question by:mattyd247
  • 3
4 Comments
 
LVL 2

Accepted Solution

by:
ricje earned 50 total points
ID: 22758841
Heres the code I normally use, see if that solves your expression issue:



function checkEmail($email) 

{

   if(eregi("^[a-zA-Z0-9_]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$]", $email)) 

   {

      return FALSE;

   }
 

   list($Username, $Domain) = split("@",$email);
 

   if(getmxrr($Domain, $MXHost)) 

   {

      return TRUE;

   }

   else 

   {

      if(fsockopen($Domain, 25, $errno, $errstr, 30)) 

      {

         return TRUE; 

      }

      else 

      {

         return FALSE; 

      }

   }

}

Open in new window

0
 
LVL 2

Expert Comment

by:ricje
ID: 22758858
obviously handy as it checks the mx record of the domain as well
0
 

Author Comment

by:mattyd247
ID: 22758972
Thanks for the suggestion but I'm getting this now:

Call to undefined function getmxrr()

Ideally I'd like to just work out what is missing from my own regular expression so that it accepts hyphens in the email address.
0
 
LVL 2

Expert Comment

by:ricje
ID: 22761348
maybe im just tired but Ive run your code with a email with a '-' char in it. Seems to work


<?php
 
 

// check for a valid email address

function check_email_address($email) {

// First, we check that there's one @ symbol, and that the lengths are right

if (!ereg("^[^@]{1,64}@[^@]{1,255}$", $email)) {

// Email invalid because wrong number of characters in one section, or wrong number of @ symbols.

return false;

}

// Split it into sections to make life easier

$email_array = explode("@", $email);

$local_array = explode(".", $email_array[0]);

	for ($i = 0; $i < sizeof($local_array); $i++) {

		if(!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$", $local_array[$i])) {

		return false;

		}

	}

	if (!ereg("^\[?[0-9\.]+\]?$", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name

	$domain_array = explode(".", $email_array[1]);

		if (sizeof($domain_array) < 2) {

			return false; // Not enough parts to domain

		}

		for ($i = 0; $i < sizeof($domain_array); $i++) {

			if (!ereg("^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$", $domain_array[$i])) {

			return false;

			}

		}

	}

return true;

}

 
 
 

$email="matt-stephen.down@domain.com";

$test=check_email_address($email);
 

if($test==1)

{

print "this email is valid";

}

else

{

print "this email is not valid";

}
 
 

?>

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now