Page 1 of 1
help with email regex
Posted: Fri Sep 22, 2006 2:14 pm
by s.dot
Code: Select all
//invalid email address?
if(!preg_match("^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*".
"\.(([0-9]{1,3})|([a-zA-Z]{2,6})|(aero|coop|info|museum|name))$",$_POST['email']))
{
header('Location: mailing-list-join.php?err=2');
exit;
}
My email address I enter always fails -- valid or not. I've tried several regexes I've found googling, but they always fail. Is my PHP wrong? Or is it the regex?
Posted: Fri Sep 22, 2006 2:34 pm
by Luke
this is the one I have been using... I am a regex retard, so i really don't know how well it works, but I know it works.
Code: Select all
/^[a-z0-9]+[\w\-_\.]*?[a-z0-9]@[a-z0-9]+[a-z0-9\-\.]*\.[a-z]{2,}$/i
Posted: Fri Sep 22, 2006 2:44 pm
by s.dot
D'oh! I was concentrating on finding a good regex and forgot to put delimiters on my regex. This are the kinds of things that make me look stupid in public.
I'll give that one a try too, ninja. See which one i like better. Thanks.
Posted: Fri Sep 22, 2006 2:48 pm
by s.dot
The Ninja Space Goat wrote:this is the one I have been using... I am a regex retard, so i really don't know how well it works, but I know it works.
Code: Select all
/^[a-z0-9]+[\w\-_\.]*?[a-z0-9]@[a-z0-9]+[a-z0-9\-\.]*\.[a-z]{2,}$/i
This will allow:
someuser@yahoo.bogusdomain
[edit] Apparently PHPBB does, too.
[edit number 2] If you change {2,} to {2,6} I think it will be the most accurate you can get. I don't think there are any 7 letter TLDs.
Posted: Fri Sep 22, 2006 2:59 pm
by feyd
Why not use the RFC compliant regex that has been posted many times?
http://svn.gna.org/viewcvs/blacknova/tr ... iew=markup
Posted: Fri Sep 22, 2006 3:06 pm
by s.dot
Aren't there valid emails that are not RFC compliant?
IE: emails that start with a hyphen.
I've seen some emails like -.
superman.-@hotmail.com
[edit] I'm entirely guessing that that email is non-rfc compliant.
Posted: Fri Sep 22, 2006 3:38 pm
by feyd
Test it.

Email Regex
Posted: Mon Dec 18, 2006 12:36 pm
by Draken Stark
Well here's a regex that my class and I made for a Perl lab that we did. Works with the large majority that's out there. We have yet to find anything wrong with it other than the top level domain area. (e.g. for instance you could have an email like this:
al_fred@woopiedoo.superman . If it's not obvious the (dot)superman shouldn't exist unless some higher up in the government wanted it to or whatever... lol)
Edit: the previous regex code I had here had allowed some obviously invalid emails (example: whatever@second@at_symbol.com )
Here's the code that we had just recently optimized for a PHP Helpdesk project.
^([A-Za-z0-9_\+\-])+(\.[A-Za-z0-9_\+\-]+)*@([A-Za-z0-9_\+\-]+(\.[A-Za-z0-9_\+\-]+)*\.([A-Za-z0-9]){2,})$
Top level domain list
Posted: Tue Jan 23, 2007 11:58 am
by Draken Stark
For those of you who wish to only be able to have valid top level domains check out this site:
http://data.iana.org/TLD/tlds-alpha-by-domain.txt
Feel free to be my guest to add them all >.< lol there's so many.