0

I'm currently trying to validate email address using regular expression in JavaScript. These are the requirements of the Email address:

  1. The email field contains a user name part follows by "@" and a domain name part.
  2. The user name contains word characters including hyphen ("-") and period (".").
  3. The domain name contains two to four parts of alphabet characters word extension.
  4. Each word extension is separated by a period (".") and the last extension must have two to three characters.

Among four requirements, the third one is most confusing to me. I will be very appreciate if someone can help me. Thank you.

I have tried the first answer in this page, but this answer accept even 5 or more extensions, so it doesn't meet my third requirement.

Community
  • 1
  • 1
Jade_W
  • 13
  • 1
  • 5

1 Answers1

1

For Javascript, here is the regex you need which follows the RFC 5322 standard:

/^[-a-z0-9~!$%^&*_=+}{\'?]+(\.[-a-z0-9~!$%^&*_=+}{\'?]+)*@([a-z0-9_][-a-z0-9_]*(\.[-a-z0-9_]+)*\.(aero|arpa|biz|com|coop|edu|gov|info|int|mil|museum|name|net|org|pro|travel|mobi|[a-z][a-z])|([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}))(:[0-9]{1,5})?$/i

Side note: it is better to use a very weak regex (basically just matching the '@') and sending a confirmation email.

Mate Hegedus
  • 2,887
  • 1
  • 19
  • 30