validate an email adress

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

adixtopix
Forum Newbie
Posts: 17
Joined: Thu Jun 23, 2005 7:02 am

validate an email adress

Post by adixtopix »

okay, i need a way to validate an email adress, a function

not the ones from the php.net

they do not work as i wish
one of them validates an email even if it looks like a@a.
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

The following URL will help you to define your regex:

http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

That's got to be the longest regex I've ever seen! How bout the nice simple regex in the snippets section:

viewtopic.php?t=34883
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

neophyte wrote:That's got to be the longest regex I've ever seen! How bout the nice simple regex in the snippets section:

viewtopic.php?t=34883
That nice simple regex is not RFC compliant, and will fail on *correct* email addresses.

Don't blame the regex. Blame the RFC for allowing truly insane and outlandish email addresses. How about the ever-so-simple question of how LONG an email address can be?

Would you believe it can be *over* 255 characters long?

Yeah. The RFC is insane. The correct regex simply codifies the insanity from the RFC.

Don't take shortcuts.. while its a long regex, its actually amazingly fast. The function we use (I promise I'll post a link to it as soon as its up) uses a php variant of the official regex from the ultimate regex book. Despite its size, it executes in fractions of a second.

I'd rather be accurate and longer, than inaccurate and shorter.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

I have written a script that actually connects to the remote mailserver to see if it will accept mail for an address all done in real time while the client is still filling in the form. Here's a demonstration.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

d11 pointed me to this thread and omg...holy mother of regex and all things regexableish 8O

could someone please give me a line-by-line explanation of what that does? I don't get it 8)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

bokehman wrote:I have written a script that actually connects to the remote mailserver to see if it will accept mail for an address all done in real time while the client is still filling in the form. Here's a demonstration.
It doesn't work for me...

Enter an email: timvw@users.sourceforge.net
Warning: The email entered does not appear to be valid.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

timvw wrote:It doesn't work for me...
Well, well, well... I don't believe it. Sourceforge did a check to see if I have a postmaster account set up at my mail server and rejected the mail because I didn't.

550-Postmaster verification failed while checking <mailer-daemon@myhomewebserver.co.uk>

Anyway I have side stepped it now by using the 'mail to' as the 'mail from' address. Anyway try it now.

Thanks for finding that bug.
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Roja wrote:That nice simple regex is not RFC compliant, and will fail on *correct* email addresses.
And again:
Roja wrote:Don't take shortcuts.. while its a long regex, its actually amazingly fast. The function we use (I promise I'll post a link to it as soon as its up) uses a php variant of the official regex from the ultimate regex book. Despite its size, it executes in fractions of a second.
I'm just waiting for Roja to post the mother of all REGEX for email validation that works for PHP. Until he does, I'm sticking with the simple stuff that works on *most* email address. Although, I hate it when people try to get fancy and throw dots into the mix.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

Burrito wrote:could someone please give me a line-by-line explanation of what that does? I don't get it 8)
Well the XML Http request object can send requests to and from the server without a page reload and when this is combined with javascript returned information can be dynamically intergrated into the page. The back end of this is just a PHP script using fsockopen() to interogate the remote mail server.
User avatar
bokehman
Forum Regular
Posts: 509
Joined: Wed May 11, 2005 2:33 am
Location: Alicante (Spain)

Post by bokehman »

neophyte wrote:post the mother of all REGEX for email validation that works for PHP. Until he does, I'm sticking with the simple stuff that works on *most* email address. Although, I hate it when people try to get fancy and throw dots into the mix.
This is nothing to do with REGEX. It is a real time connection with the remote mail server to see if it will accept mail for an address it has authority for.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

bokehman wrote:
Burrito wrote:could someone please give me a line-by-line explanation of what that does? I don't get it 8)
Well the XML Http request object can send requests to and from the server without a page reload and when this is combined with javascript returned information can be dynamically intergrated into the page. The back end of this is just a PHP script using fsockopen() to interogate the remote mail server.
actually I meant the mother of all regex's (/me pulls tongue out of his cheek)...I didn't look at your thingy.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

neophyte wrote: I'm just waiting for Roja to post the mother of all REGEX for email validation that works for PHP.
Sorry for the delay. Its up!

http://svn.gna.org/viewcvs/blacknova/tr ... iew=markup

Very simple. Pass it an RFC compliant email, and it returns true. Pass it bogus info, and it returns false.

Patches, comments, and rude limericks all welcome.
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post by Roja »

bokehman wrote:This is nothing to do with REGEX. It is a real time connection with the remote mail server to see if it will accept mail for an address it has authority for.
So which method are you using to ask it that? VRFY?
Post Reply