0

I'm using the following for email validation:

var filter = /^(\w+)(\.\w+)*@(\w+)(\.\w+)+$/;

Just noticed that it does not support xxxx+wildcard@gmail.com (it does not support the +wildcard part). Any way to get that added?

Crescent Fresh
  • 115,249
  • 25
  • 154
  • 140
AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012
  • 1
    It also doesn't support other common email characters like numbers, dashes, or underscores. – Mike M. Lin Mar 06 '11 at 03:49
  • My bad. `\w` does include numbers and underscores, but does it include dashes? Example: `an.apprentice@experts-exchange.com` – Mike M. Lin Mar 06 '11 at 06:37
  • see also http://stackoverflow.com/questions/4727145/regex-for-email-validation-not-working-with-subdomains/4727363#4727363 – Christoph Mar 06 '11 at 10:48

2 Answers2

2

You should use \S+@\S+\.\S+, which will match anything with an @ and a ..
Anything more than that will reject valid but obscure addresses.

Even this will reject valid but obscure addresses, such as "Test Me"@localhost.
However, these are never used in practice. [citation needed]

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
1

I liked SLak's answer. I actually have a regular expression I wrote awhile back that's even more open-ended.

^.+@.+\..+$

The idea behind it is similar. Don't try so hard. Instead, err on the side of accepting too much. To my knowledge this regex should accept every conceivable valid email address (and some invalid ones as well).

Steve Wortham
  • 21,740
  • 5
  • 68
  • 90