[SOLVED] Validating a email

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cheddar
Forum Newbie
Posts: 7
Joined: Tue Nov 25, 2003 4:23 pm
Location: UK

[SOLVED] Validating a email

Post by cheddar »

Does anyone know how I would go about validating a email address. I have a form that the user enters their email on, if at all possible I would like to check if the email is valid.

Example:

cheddar@there.net is valid
cheddar.cheese@mature.net is also valid
cheddar.cheese is not valid

I have found what I thought was a great little expression to do this but when I run it I get the following error@

"Warning: eregi(): REG_ERANGE: in c:\program files\apache group\apache\htdocs\learn\core.php on line 28"

I am verifying the email as follows:

Code: Select all

function is_email_valid($str) 
{ 
// checks to see if a valid email format has been entered
  if(eregi('^[ a-z0-9-_]+(.[ a-z0-9-_]+)*@[a-z0-9-]+(.[a-z0-9-_]+)*(.[a-z]{2,3})$', $str)) 
  return true;
  else 
      return false;;
}

// check to see if the email is valid
	if (!is_email_valid($email))
	{
		$error="Please enter a valid email";
		return($error);
	}
This code works but I still get the error above, anyone know whats going on or a better way to do this?

Thanks.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

http://www.zend.com/tips/tips.php?id=8&single=1

might help, found it with the google search query
"validating email address PHP"
cheddar
Forum Newbie
Posts: 7
Joined: Tue Nov 25, 2003 4:23 pm
Location: UK

Post by cheddar »

Thanks for the help, it's working now
Post Reply