4

I want it to be correct my JavaScript regex pattern to validate below email address scenarios

  1. msekhar@yahoo.com
  2. msekhar@cs.aau.edu
  3. ms.sekhar@yahoo.com
  4. ms_sekhar@yahoo.com
  5. msekhar@cs2.aau.edu
  6. msekhar@autobots.ai
  7. msekhar@interior.homeland1.myanmar.mm
  8. msekhar1922@yahoo.com
  9. msekhar#21@autobots.com
  10. \u001\u002@autobots.com

I have tried the following regex pattern but it's not validating all the above scenarios

/^[_a-z0-9]+(\.[_a-z0-9]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$/

Could any one please help me with this where am doing wrong?

sekhar
  • 111
  • 1
  • 1
  • 10
  • 1
    Seems to be a duplicate question! Did you check the following?- https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript – Marcus Apr 05 '19 at 09:40
  • 2
    Possible duplicate of [How to validate an email address in JavaScript?](https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript) – jayarjo Apr 05 '19 at 09:41
  • Possible duplicate of [JavaScript Regular Expression Email Validation](https://stackoverflow.com/questions/940577/javascript-regular-expression-email-validation) – Shibon Apr 05 '19 at 09:46
  • Note, the the aforementioned referenced topics do not take into account unicode characters in the local part. See also SMTPUTF8 in [RFC6531](https://tools.ietf.org/html/rfc6531) – Marcus Apr 05 '19 at 09:53

3 Answers3

2

The following regex should do:

^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$

Test it: https://regex101.com/r/7gH0BR/2

EDIT: I have added all your test cases

Marcus
  • 1,097
  • 11
  • 21
0

I have always used this one but note it doesn't trigger on escaped unicode:

^([\w\d._\-#])+@([\w\d._\-#]+[.][\w\d._\-#]+)+$

You can see how it works here: https://regex101.com/r/caa7b2/4

Sv443
  • 708
  • 1
  • 7
  • 27
  • The use of international characters in the local-part have been introduced in 2012 as part of [RFC6531](https://tools.ietf.org/html/rfc6531) – Marcus Apr 05 '19 at 10:00
0

First off [_a-z0-9]+ is going to match the username fields for the majority of those testcases. Anything further testing of username field content will result in a mismatch. If you write a pattern that expects two .-delimitered fields, it'll match when you provide two .-delimitered fields and only then, not anything else. Make a mental note of that. I think you probably meant to put the . in the first character set, and omit this part here: (.[_a-z0-9]+)...

As for the domain part of the email address, similar story there... if you're trying to match domains containing two labels (yahoo and com) against a pattern that expects three... it's going to fail because there's one less label, right? There are domain names that only contain one label which you might want to recognise as email addresses, too, like localhost...

You know, there is a point to where you can dig yourself down a very deep rabbit hole trying to parse email addresses, much to the effect of this question and answer sequence. If you're making this complex using regular expressions... I think maybe a better tool is a proper parser generator... otherwise, write the following:

  1. A pattern that matches anything up until an @ character
  2. A pattern that matches the @ character (this will help you learn how to avoid your .-related error)
  3. A pattern that matches everything (this will help you understand your .-related error)
  4. Combine the three above in the order presented.