RegEx validate name and email

Viewed 51

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

1 Answers

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}$
Related