validating the syntax of an email address

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
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

validating the syntax of an email address

Post 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
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

This question has been answered many many times before.
Post Reply