6

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:

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

2

1 Answer 1

16

This is the regex which does what you want:

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

9
  • 1
    If you think this is a duplicate question, mark it as such and don't copy answer.
    – Toto
    Commented Dec 4, 2018 at 10:58
  • I have tried this in my Angular app and it fails to match any emails
    – Node_Ninja
    Commented Dec 4, 2018 at 11:47
  • Please create a stackblitz as I think the problem is not with the regex. Commented Dec 4, 2018 at 12:04
  • Please find the code here: stackblitz.com/edit/angular-edgene
    – Node_Ninja
    Commented Dec 4, 2018 at 12:33
  • 1
    Hi. I edited my earlier answer, now the regex will work. Commented Dec 4, 2018 at 15:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.