Page 1 of 1

[SOLVED] Validating a email

Posted: Wed Nov 26, 2003 5:33 pm
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.

Posted: Wed Nov 26, 2003 6:41 pm
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"

Posted: Thu Nov 27, 2003 12:43 pm
by cheddar
Thanks for the help, it's working now