Regular expression in php of 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

nasir
Forum Newbie
Posts: 5
Joined: Thu Dec 07, 2006 7:36 am

Regular expression in php of email address

Post by nasir »

i need regular expression in php for validation of email field.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I need you to search, please.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

I need a cheese pizza. And a coke. 8)
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Re: Regular expression in php of email address

Post by dibyendrah »

nasir wrote:i need regular expression in php for validation of email field.
I guess, you may find hundreds of regular expression dealing with email validations on Regex discussion
viewforum.php?f=38
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

:dubious: I was going to hold out for toppings first!
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post by dhrosti »

This is my first post on this forum, so please forgive me if this code doesnt turn out right...

Code: Select all

ereg("[_a-zA-Z0-9.\'-]*@[_a-zA-Z0-9-]*\.[_a-zA-Z0-9-]*", $email)
This code wont accept anything unless it has exactly one @ character and at least one dot.

Dan.
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Code: Select all

<?php
$email = "a.b@c.com";
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
	echo "The e-mail was not valid";
} else {
	echo "The e-mail was valid";
}
?>
For email as $email = "a.b@c.com";

Code: Select all

The e-mail was valid
For email as $email = "a.b@cnet";

Code: Select all

The e-mail was not valid
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Too bad a.b@cnet is a valid email address ;)
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

feyd wrote:Too bad a.b@cnet is a valid email address ;)
But the extra info like .com or .net or some country code .com.np is missing. That is why it's invalid. Isn't it feyd ? I might be wrong.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

dibyendrah wrote:But the extra info like .com or .net or some country code .com.np is missing. That is why it's invalid. Isn't it feyd ? I might be wrong.
Unfortunately, you are wrong. :)

The RFC that governs email addresses does not require a dot to appear in the domain part.

validateEmailFormat()
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

8O

Doesn't that violate the "domain name" model?
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

feyd wrote:
dibyendrah wrote:But the extra info like .com or .net or some country code .com.np is missing. That is why it's invalid. Isn't it feyd ? I might be wrong.
Unfortunately, you are wrong. :)

The RFC that governs email addresses does not require a dot to appear in the domain part.

validateEmailFormat()
validateEmailFormat() given in your link was preety complex just to validate email address. I think that kind of email exist only on intranet. root@localhost or someuser@some.ip.address .
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

It's still RFC compliant. I have yet to see anyone else post one. :)
User avatar
dibyendrah
Forum Contributor
Posts: 491
Joined: Wed Oct 19, 2005 5:14 am
Location: Nepal
Contact:

Post by dibyendrah »

Can we make that kind of email without any .com or .org ... ?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Yep.
Post Reply