-1

I have this regex for email

let text = "abc@gmail.com";
let pattern = /^[_a-z0-9-]+(\.[_a-z0-9-]+)*(\+[a-z0-9-]+)?@[a-z0-9-]+(\.[a-z0-9-]+)$/i;
console.log(pattern.test(text))

but if it's abc@mit.edu.us it will fail, which part should I change?

Jenny Le
  • 3
  • 1
  • 1
    if you add a `+` at the end (...`(\.[a-z0-9-]+)+$`) it will allow multiple subdomains – Diego De Vita May 11 '22 at 05:57
  • Subdomains aren't your only problem, you're disallowing a ton of other valid addresses. Bottom line: regexen are mostly useless for validating email addresses to begin with. At best, use `.+@.+`, and then you need to send an email with a validation link anyway to make sure the email actually exists and actually belongs to the user. – deceze May 11 '22 at 06:22

0 Answers0