How to validate an email address in JavaScript (2020)

Email validation is hard. With the vast amount of complex, but valid email addresses that exist today, the only way to truly tell if an email address is valid is to send the email and see if it bounces. With that said, there are a few things we can do on the front end to make the experience better for everyone. In this post we’ll talk about few common approaches for validating email addresses in JavaScript.

First, when validating email addresses I believe it’s better to error on the permissive side. I’d much rather let pass a few fake email addresses than reject a valid one. Front-end email validation is about figuring out if the syntax is correct, not if the email address is valid. With that said, we’ll start off with the most permissive solution and work our way towards the other end.

function emailIsValid (email) {
  return /\S+@\S+\.\S+/.test(email)

If you’re not familiar with RegEx, /\[email protected]\S+\.\S+/ is testing for the most basic email address structure, [email protected]_._. That’s typically all I ever assume about an email address. If the email the user types in doesn’t follow that structure, odds are they made a typo.

One issue with the Regex above is it’ll fail on this format: [email protected][email protected]_. To fix that, we can change up our Regex a bit.

function emailIsValid (email) {
  return /^[^\[email protected]]+@[^\[email protected]]+\.[^\[email protected]]+$/.test(email)

emailIsValid('[email protected]') // false
emailIsValid('[email protected]') // true

That’s it. Anything beyond this is going to be too opinionated. You could find some other solutions by googling around but I recommend re-thinking your approach if the above examples don’t work for you.

One more thing

Hear me out – most newsletters are trash. At best they're boring and at worst they're spam.

With Bytes, we wanted to create something you actually looked forward to reading. It's like if you combined tc39 meeting notes with an Old Spice commercial.

I pinky promise you'll love it, but here are some recent issues so you can decide for yourself.

38,437 Subscribers. Every Monday.