tags:

views:

45

answers:

4

I have the following PHP code that checks if an email is valid:

if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2}[mtgvu]?$", $email))
    return true;

Works great but I found out today that if I try validate a .info email, it says it's invalid. Any ideas what I need to add/modify to make it return .info emails as valid?

Thanks

Ben

A: 

To just allow .info as well:

if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2}[mtgvu]?(fo)?$", $email))

(added optional 'fo' to end)

mwalker
It's `[mtgvuo]?`, so the last character is optional, which means it would work for country codes.
casablanca
true... it's the [a-z]{2}[mtgvu]? that limits it to three characters. So country codes fit in the [a-z]...
mwalker
Ah. no .museum? And ICANN was thinking about selling new arbitrary TLD's a while back, I don't know how far along they are with that.
Wrikken
I think casablanca's fo-fu is cleaner.
mwalker
+2  A: 

Use:

if(filter_var($email, FILTER_VALIDATE_EMAIL)) echo 'yeey, valid';

If interested in the actual regex it uses: it's in the public php source ( http://gcov.php.net/PHP_5_3/lcov_html/filter/logical_filters.c.gcov.php, line 525)

Wrikken
I want to allow PHP 4 support so I wont be able to use this. I've changed my code to preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2}([mtgvu]|fo)?$", $email, $matches);
Ben Sinclair
In that case, why not expect the future to happen, use filter_var if available, and for consistency use the regex I pointed out to you?
Wrikken
+1  A: 

The [a-z]{2}[mtgvu]? at the end means that the top-level domain can only be two or three characters long. If you want a quick hack to make it accept info, you can change [mtgvu]? to ([mtgvu]|fo)?:

if (eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@([0-9a-z][0-9a-z-]*[0-9a-z]\.)+[a-z]{2}([mtgvu]|fo)?$", $email))
    return true;
casablanca
Is there anyway to make it so any TLD is valid? this way when new ones come out, it will work.
Ben Sinclair
Just replace this part at the end: `[a-z]{2}([mtgvu]|fo)?` with `[a-z][a-z]+` - this will match any TLD that is 2 or more characters. Or if you want to restrict it to at most 4 characters, use `[a-z]{2,4}`.
casablanca
A: 

Please consider using a more RFC-compliant email parser, like this one from Dominic Sayers, or this one from Cal Henderson of Flickr.

Charles