Help - Search - Members - Calendar
Full Version: Check e-mail validity
Invision Power Services > Community Forums > Community Web Design and Coding
Sebastian Mares
Hi!

Does anyone know a RegEx for checking if an e-mail is valid? It should be compatible with the following formats:

mailto:test@domain.tld
test@domain.tld
test_test.test@domain.tld
test@subdomain.domain.tld
test@123.123.123.123
...

Of course, it should return false for:

_test@domain.tld
test@subdomain1.subdomain2.domain.tld
a@b.c
...

I tried creating my own, but I am not very good at RegEx:

CODE
/^(?mailto:)?[A-Za-z0-9][A-Za-z0-9_\.\-]@[([A-Za-z0-9][A-Za-z0-9_\.\-])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})]\.[A-Za-z]{2,5}$/


The above doesn't seem to work well with subdomains and I am sure it has other problems since it's written by me. tongue.gif

Regards,
Sebastian
Sebastian Mares
Does anyone know a good method for checking if an address really is valid? I tried playing around with TELNET and VRFY and MAIL/RCPT but most of the servers I tried have VRFY disabled and MAIL only works after either logging in or using POP3 before.
princetontiger
The simplest method would be to just send a validation E-Mail, like most software currently does.

You could check out the regex library: http://www.regexlib.com/DisplayPatterns.aspx ... I wrote a simple regex E-Mail validation string, but it's nothing as complex as what you want.
Brendon Koz
Why do you want to disallow subdomain email addresses?

I used the following guideline to update my own email checking function, and then ended up updating this code alongside my code to create a final product. As I can't currently connect to my server (I'm at work and forgot password) I can't give you my code, but you can use this to get a base understanding:

http://www.spoono.com/php/tutorials/tutorial.php?id=41

Just a note on Email Verification:
It's sometimes rather difficult to come up with "the perfect" email verifier. However, denying SOME LEGAL visitors to your site is much, much worse than allowing SOME ILLEGAL (email formedness) visitors to your site.

Hope that helps get you in the right direction.

P.P.S. - I wouldn't personally use the domain checking unless you're completely paranoid, however I do keep it in my code (turn it on/off via a $GLOBAL). You also cannot be on a Windows' server for that domain check to work properly.
Sebastian Mares
QUOTE(Transverse Styles @ Jun 14 2005, 12:43 AM) *
The simplest method would be to just send a validation E-Mail, like most software currently does.

You could check out the regex library: http://www.regexlib.com/DisplayPatterns.aspx ... I wrote a simple regex E-Mail validation string, but it's nothing as complex as what you want.


I can't do that since I need the validation thingy for a feedback form.

QUOTE(malikyte @ Jun 14 2005, 12:44 AM) *
Why do you want to disallow subdomain email addresses?


Who said I want to do that? It's that my RegEx doesn't allow subdomains which is a bug.
Anyways, I was thinking about implementing something like this: http://gemal.dk/browserspy/mailcheck.php
The MX record part is not so difficult since I can use getmxrr (the server is on Linux), otherwise I could rely on nslookup.
Brendon Koz
The mxlookup isn't difficult in either example...it's just that it adds extra (possibly unnecessary) processing time; however, that's your call. original.gif
_
http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html

Sorry, I had to post that. fear.gif
Sebastian Mares
Thanks Veracon, I saw that before and have to say that it looks pretty - umm - confusing. laughing.gif
_
Yeah, it's also very slow in anything but Perl. :/
Barn
What's wrong with double subdomains?
Sebastian Mares
QUOTE(Barn @ Jun 15 2005, 09:42 PM) *
What's wrong with double subdomains?


Didn't know that such things even exist / are valid.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.