2

I want to validate email format with name that can handle both patterns

Valid

  1. test@domain.com
  2. test user <test@domain.com>
  3. test user<test@domain.com>
  4. "test user" <test@domain.com>
  5. "test user"<test@domain.com>

Invalid

  1. "test user" <test@domain.com
  2. "test user" test@domain.com>
  3. test user <test@domain.com
  4. test user test@domain.com>
  5. "test user <test@domain.com>
  6. test user" <test@domain.com>
  7. "test user" test@domain.com

here's my progress: https://regex101.com/r/Q00McL/1

sic
  • 69
  • 1
  • 3
  • 3
    Does this answer your question? [How to validate an email address in JavaScript](https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript) – tsamridh86 Jul 27 '21 at 03:08
  • @SamridhTuladhar OP's requirements are slightly different – Spectric Jul 27 '21 at 03:10
  • It is correct that the last one `"test user" test@domain.com` should be valid, like `^(?:(?:"[^"\r\n]+"|\w+(?: +\w+)*) *)?\w+@[^<>\s@]+\.\w{2,3}$` https://regex101.com/r/lPYQL6/1 – The fourth bird Jul 27 '21 at 07:03

1 Answers1

0

Why are items 4. "test user" test@domain.com (valid) and 7. "test user" test@domain.com (invalid) the same?

If you want "test user" test@domain.com to be valid use this pattern:

^(?:(?:"[^"\r\n]+"|\w+(?: +\w+)*) *)?\w+@[^<>\s@]+\.\w{2,3}$

On the contrary, if "test user" test@domain.com should be invalid use:

^(?:(?:"[^"\r\n]+"|\w+(?: +\w+)*)*)?\w+@[^<>\s@]+\.\w{2,3}$
DVN-Anakin
  • 1,549
  • 1
  • 8
  • 12