Page 1 of 1

validating the syntax of an email address

Posted: Tue Aug 08, 2006 5:01 am
by jasongr
Hi people

can someone supply with a PHP code that checks whether a given PHP string
constitutes a legal email address in terms of the syntax?
I am sure regular expressions are the way to go.
I found the following solution:

Code: Select all

function isValidEmailSyntax($email) {
     if(ereg("^([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[@]([0-9,a-z,A-Z]+)([.,_,-]([0-9,a-z,A-Z]+))*[.]([0-9,a-z,A-Z]){2}([0-9,a-z,A-Z])*$",$email)) {
	return true;
     } else {
	return false;
     }
}
But when I pass the value 'john@yahoo.com' it says that the email address is NOT valid.
Does that sound right?
Can anyone see the bug in the function?

regards

Posted: Tue Aug 08, 2006 6:41 am
by Chris Corbyn
This question has been answered many many times before.