How can I validate an email address in JavaScript?

Created by John RutherfordLinked to 98.7m issues across 240 teams

tl;dr

You can use a regular expression to validate an email address in JavaScript. For example, the following regular expression can be used to validate an email address:

var re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; if (re.test(email)) { // Email address is valid } else { // Email address is invalid }

The regular expression checks for a valid email address format, including the @ symbol and a valid domain name. If the regular expression returns true, then the email address is valid. If it returns false, then the email address is invalid.