Solved

php email validation problem

Posted on 2008-10-20
4
148 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Store image url and display it for user profile 6 23
WordPress Themes 10 35
Should I use subdomains or addon domains? 3 39
datetime in sql 6 31
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

863 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

25 Experts available now in Live!

Get 1:1 Help Now