views:

216

answers:

3

Hi guys,

First post here. I'm been trying to right a regular expression that checks if an email form field contains a free email address, with no luck.

Essentially i need to check if the email contains, hotmail, yahoo, gmail etc in the email field.

Any help much appreciated.

Using PHP/Drupal.

regards

H

+1  A: 
/\@(hotmail|yahoo|gmail)\.com/
stillstanding
A: 

Have you tried a big ol' disjunction?

i.e.

yahoo\.com|googlemail\.com|blah\.net

Pros: Easy to build, easy to read, explicit.

Cons: Maybe not the most efficient mechanism.

Brabster
Actually if it were using Perl v5.10 it would be very efficient. That's because it becomes a trie.
Brad Gilbert
Hmmm, not come across a 'trie' before - very interesting, thanks for the comment!!
Brabster
A: 

regex would be sth like:

[a-zA-Z0-9_\.+]+@(gmail|yahoo|hotmail)(\.[a-z]{2,3}){1,2}

you can add all the other domains you want... does it help you?

PierrOz
Obviously, the trick is how to get the list of 'all the other domains'.
Tadeusz A. Kadłubowski
This worked first time, thanks for all the answers
Can't vote up as I'm new :-)
@hixster, you *can* accept an answer, can't you? EDIT: I voted up your question, so now you have enough points to up-vote anyone.
Bart Kiers
Cool, accepted.. Thanks again!
-1: `[email protected]` and I pass through your regular expression as using a paid service. Please people, don't forget `+` is a valid character in an email address.
Andrew Moore
Also, [email protected] is a valid alias for [email protected], so this will pass through the above regex as well.
prometheus
@prometheus: `.co.uk` addresses are properly parsed by the above regex. Notice the `(\.[a-z]{2,3}){1,2}`... (dot)(2 to 3 letters)(repeated 1 or twice).
Andrew Moore
O.K how do I prevent .co.uk getting though as well?
@Andrew, so what about [email protected]? And so on ...
prometheus
@Tadeusz: I'm not sure you can find the domains automatically without a config list or sth like that. At least, I thought the question was more about the regex (cf. tag)@Andrew: my bad... but the -1 is tough, sniff... :(
PierrOz
@hixster: haha, that's a slippery slope I mentioned before. You'll be worrying about free email in Paraguay just after dealing with UK. This problem cannot be solved reliably. How much money is your client willing to sink in annoying gmail users that might want to deal with the system?
Tadeusz A. Kadłubowski
@PierrOz: Exactly, there must be some manually created config list. And the config list always will be incomplete. An incomplete solution to a pointless problem specified by some Pointy-Haired-Boss.
Tadeusz A. Kadłubowski
@PierrOz: fact is, you don't even need to check what's before the @... Personally, I would simply go for `@(yahoo|google|hotmail)(\.[a-z]+)+$`
Andrew Moore
i'd drop the `(\.[a-z]+)` and change the entire expression to a simple `/\@(yahoo|google|hotmail)\./` so it can catch `.co.uk`
stillstanding