Page 1 of 2

Regular expression in php of email address

Posted: Tue Jan 09, 2007 11:13 pm
by nasir
i need regular expression in php for validation of email field.

Posted: Wed Jan 10, 2007 12:08 am
by feyd
I need you to search, please.

Posted: Wed Jan 10, 2007 1:39 am
by Kieran Huggins
I need a cheese pizza. And a coke. 8)

Re: Regular expression in php of email address

Posted: Wed Jan 10, 2007 4:10 am
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

Posted: Wed Jan 10, 2007 4:50 am
by Kieran Huggins
:dubious: I was going to hold out for toppings first!

Posted: Wed Jan 10, 2007 5:08 am
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.

Posted: Thu Jan 11, 2007 12:56 am
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

Posted: Thu Jan 11, 2007 1:27 am
by feyd
Too bad a.b@cnet is a valid email address ;)

Posted: Thu Jan 11, 2007 3:34 am
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.

Posted: Thu Jan 11, 2007 9:56 am
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()

Posted: Thu Jan 11, 2007 11:17 am
by Kieran Huggins
8O

Doesn't that violate the "domain name" model?

Posted: Fri Jan 12, 2007 7:40 am
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 .

Posted: Fri Jan 12, 2007 10:05 am
by feyd
It's still RFC compliant. I have yet to see anyone else post one. :)

Posted: Sat Jan 13, 2007 10:31 am
by dibyendrah
Can we make that kind of email without any .com or .org ... ?

Posted: Sat Jan 13, 2007 10:38 am
by feyd
Yep.