-1

I am trying to validate email addresses using a regex. This is what I have now ^([\w-.]+)@([\w-]+).(aero|asia|be|biz|com.ar|co.in|co.jp|co.kr|co.sg|com|com.ar|com.mx|com.sg|com.ph|co.uk|coop|de|edu|fr|gov|in|info|jobs|mil|mobi|museum|name|net|net.mx|org|ru)*$ I found many solutions using non-capturing groups but did not know why. Also can you tell me if this is the correct regex and also if there are any valid emails which are not being validated correctly and vice-versa

1 Answers1

2

Don’t bother, there are many ways to validate an email address. Ever since there are internationalized domain names, there’s no point in listing TLDs. On the other hand, if you want to limit your acceptance to only a selection of domains, you’re on the right track. Regarding your regex:

  • You have to escape dots so they become literals: . matches almost anything, \. matches “.”
  • In the domain part, you use [\w-] (without dot) which won’t work for “@mail.example.com”.
  • You probably should take a look at the duplicate answer.
  • This article shows you a monstrous, yet RFC 5322 compliant regular expression, but also tells you not to use it.

I like this one: /^.+@.+\...+$/ It tests for anything, an at sign, any number of anything, a dot, anything, and any number of anything. This will suffice to check the general format of an entered email address. In all likelihood, users will make typing errors that are impossible to prevent, like typing john@hotmil.com. He won’t get your mail, but you successfully validated his address format.

In response to your comment: if you use a non-capturing group by using (?:…) instead of (…), the match won’t be captured. For instance, all email addresses have an at sign, you don’t need to capture it. Hence, (john)(?:@)(example\.com) will provide the name and the server, not the at sign. Non-capturing groups are a regex possibility, they have nothing to do with email validation.

Community
  • 1
  • 1
dakab
  • 5,379
  • 9
  • 43
  • 67
  • So it is really tough to validate false when user makes such kind of typos right? Or is there a solution for that.. – user3734389 Jul 24 '15 at 21:14
  • @user3734389: It’s really tough. You can tackle this challenge by checking for the most common email provider domain names (like gmail, yahoo, outlook), but that’s far from being a guarantee. Just check the general form and/or ask your users if they entered it correctly. – dakab Jul 24 '15 at 21:24
  • Can you tell me what is the use of non-capturing groups? I found a lot of solutions on stack overflow using non-capturing groups for email validation. Also the regex what I have written will be good for the domains I specified right? – user3734389 Jul 27 '15 at 21:46
  • @user3734389: Yes, please take a look at my answer. – dakab Jul 28 '15 at 06:24