5

I want to validate all email inputs on my app with regex. But there is a problem, all the regex I have found allow emails with no TLD. I want a regex that can help me reject emails such as testing@testing,

Examples:

  • testing@testing.com should be valid
  • testing@testing.co.us should be valid
  • testing@testing should not be valid

The current regex I use is : ^([a-zA-Z0-9_-.]+)@(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([a-zA-Z0-9-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$

But it accepts testing@testing for example, and that is not what I want. How would I go about validating emails and rejecting ones without TLD

Node_Ninja
  • 425
  • 2
  • 7
  • 18
  • Check this solution: https://stackoverflow.com/questions/46155/how-to-validate-an-email-address-in-javascript – Torben Schramme Dec 04 '18 at 10:44
  • Please, have a look at these sites: TLD list: https://www.iana.org/domains/root/db ; valid/invalid addresses: https://en.wikipedia.org/wiki/Email_address#Examples ; regex for RFC822 email address: http://www.ex-parrot.com/~pdw/Mail-RFC822-Address.html https://en.wikipedia.org/wiki/List_of_Internet_top-level_domains – Toto Dec 04 '18 at 11:00

1 Answers1

15

This is the regex which does what you want:

[A-Za-z0-9._%-]+@[A-Za-z0-9._%-]+\\.[a-z]{2,3}

Sachin Jagtap
  • 423
  • 3
  • 11