views:

404

answers:

4

When users create an account on my site I want to make server validation for emails to not accept every input.

I will send a confirmation, in a way to do a handshake validation.

I am looking for something simple, not the best, but not too simple that doesn't validate anything. I don't know where limitation must be, since any regular expression will not do the correct validation because is not possible to do it with regular expressions.

I'm trying to limit the sintax and visual complexity inherent to regular expressions, because in this case any will be correct.

What regexp can I use to do that?

+12  A: 
^\S+@\S+\.\S+$
chaos
This will match invalid addresses. Any regex will, but this one will match common mis-spellings such as [email protected] (note the double dots.) Please provide a better example.
Mihai Limbășan
It's supposed to be a maximally simple, very rough filter, and I don't see why doubled periods are privileged over all the other screwups with similar complexity costs to cover them.
chaos
+1. This is a subjective question anyway, and this is simple.
Jason Cohen
Yeah, if you don't want to use the full validating regex, this is a good simple approximation
rampion
This is a very comon screwup, but if you think the fractional microsecond cost of a better regex is not worth it... *shrug again* Retracted downvote, enjoy.
Mihai Limbășan
Heh, thanks. It's not the fractional microsecond, it's the visual/syntactic complexity of the expression; my impression was that the OP was wanting to minimize that.
chaos
+1 Trying to “validate” an e-mail address fully via regex is a fool's errand. This works to catch the simplest mis-types; the rest can be found by trying to send the mail. The above also allows Unicode (->Punycode) domains, where most “clever” regexes fail it.
bobince
+1 because it is "something simple, not the best"
dfa
I'm picky, but a dot in the domain part is not required. And this one looks simple, but its not better than no test at all. Its not much better than string search for an @ character. Check JP's answer for a _correct_ one.
frunsi
A: 

I think any regexp is by nature complex, so as twk suggests in his comment, why limit yourself to something that is not really complex, but yet not really straightforward to understand either. Go for a non-regexp solution, or consider choosing a bit more powerful regexp.

Daan
+5  A: 

It's possible to write a regular expression that only accept email addresses that follow the standards. However, there are some email addresses out there that doesn't strictly follow the standards, but still work.

Here are some simple regular expressions for basic validation:

Contains a @ character:

@

Contains @ and a period somewhere after it:

@.*?\.

Has at least one character before the @, before the period and after it:

.+@.+\..+

Has only one @, at least one character before the @, before the period and after it:

^[^@]+@[^@]+\.[^@]+$
Guffa
Hmm... WHy the downvote? If you don't specify a reason it's rather pointless...
Guffa
+6  A: 

Take your pick.

Here's the one that complies with RFC 2822 Section 3.4.1 ...

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])

Just in case you are curious. :)

JP Alioto