0

I'm using the following for email validation:

var filter = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/; // For Email Validation

if (filter.test(emailInputVal))) {console.log('good')}

For some reason the above does not work with emails that have a subdomain Any ideas why?

xxxx@xxx.xxx.com 

Thanks

AnApprentice
  • 108,152
  • 195
  • 629
  • 1,012

5 Answers5

9

Because your regular expression is incorrect. Try this instead:

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

This link may help you lots when validating email addresses:

http://www.regular-expressions.info/email.html

Official RFC 2822 standard

This non-trivial simplified regular expression conforming to RFC 2822 standard:

var filter = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)\b/;
Robert Koritnik
  • 103,639
  • 52
  • 277
  • 404
  • @AnApprentice: Don't forget to check the link I attached which shows several email cheking regular expressions and explains email validation using regular expressions in depth. – Robert Koritnik Jan 18 '11 at 17:41
  • 1
    doesn't handle quotes and will break with new top-level domains; as you'll probably send a confirmation mail anyway, why not use a less strict expression? – Christoph Jan 18 '11 at 18:02
2

That is one weird regex. It's certainly not doing what you're expecting it to do, for example because the dot isn't escaped when you do mean a literal dot.

Since it's impossible to really validate an email address with a regex anyway - why not go for something simpler?

/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$/i

This will still match some invalid addresses and will reject some valid addresses (as all readable regexes do), but in the end you have to send a confirmation mail to a user-submitted mail address and see if you get a reply if you truly want to validate it.

Tim Pietzcker
  • 328,213
  • 58
  • 503
  • 561
  • 1
    why reject valid addresses when you have to send a confirmation mail (possibly after checking the DNS record on the server-side) anyway? – Christoph Jan 18 '11 at 17:54
2

You can't reliably validate email addresses with regular expressions. What I'd do:

  • use a simple expression like /^[^@]+@([A-Za-z0-9-]+\.)*[A-Za-z0-9-]+$/ for client-side validation to catch typos
  • check the DNS record on the server-side
  • send a confirmation mail
Christoph
  • 164,997
  • 36
  • 182
  • 240
  • adding support for quoted strings slightly complicates the expression: `/^("(\\"|[^"])*"|[^@\s]+)@([A-Za-z0-9-]+\.)*[A-Za-z0-9-]+$/` should do it... – Christoph Mar 06 '11 at 11:12
0

Your last component is: any length word, then one or two instances of (dot, two-or-three-letter word). I would expect "xxxx@xxx.xxx.com" to work, but perhaps not more realistic examples like "xxxx@xxx.example.com" because your domain name is not a two-or-three-letter word.

Do yourself a favor: use simply /^[^@ ]+@[^@ ]+\.[^@ ]+$/ More about this: http://nedbatchelder.com/blog/200908/humane_email_validation.html

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
0

I am not sure if the above code will work any format, because there is an extra ) in the if condition, removing it works for the sub domain too:

if (filter.test(emailInputVal)) {console.log('good')} 
Chandu
  • 81,493
  • 19
  • 133
  • 134