There's only one way to validate an email address
Tags: webdev, devtips, regex, podcast
Published: 2019-06-18 15:16:46

---
title: There's only one way to validate an email address
canonical_url: https://changelog.com/posts/theres-only-one-way-to-validate-an-email-address
published: true
cover_image: https://thepracticaldev.s3.amazonaws.com/i/kung3vdupybqlh0t39ic.png
description: Here's some hard-earned experience on how to validate an email address. If you listened to JS Party #39, then you already know this. If you think I'm about to hand the best regex you've ever seen...
tags: webdev, devtips, regex, podcasts
---

The only thing that you can reliably do to validate an email address is to send it an email. YOU SEND IT AN EMAIL! That's the <mark>only way you can do it</mark>. I know what you're thinking, 

> "I have the best regular expression for this!" 

{% twitter 845821937452486657 %}

No, you do not. You think you do, but you don't. Your regular expression is invalid; it's not good enough. You know the old adage:

> "A developer, when faced with a problem, thought 'I know. I'll use regular expressions.' Now he has two problems." 

That's what you have - <mark>you have two problems</mark>. I've known this for years, and yet I was still convinced recently to add a regular expression-based email validation server-side; 

_(First of all, never trust a client, right? You can do it all you want there, but it can bypass all your checks. It's gotta be server-side.)_

{% twitter 834831478919987205 %}

I put a regular expression-based email validation and I thought "This one's pretty good." 

In fact -- man, I don't know what came over me; I was actually even talked into copy-pasting one off of a gist! 😭

It looked pretty good, and it covered most of the bases, and sure enough, last week I got an email from a prospective user saying 

> "Hey, I'm trying to sign up for [Changelog Weekly](https://changelog.com/weekly), but it says my email address isn't valid, and it <mark>obviously is valid</mark>, because I'm emailing you with it right now..."

And I thought, "I'm an idiot. **Why** did I put a regular expression-based email validation on my system?" 

So don't do that. I know you can find one on Stack Overflow...  I'll tell you right now, it's not good enough. Email addresses are SO complicated. There's so many valid things... 

If you're going to do it -- and I'll admit that I [kept it in there](https://github.com/thechangelog/changelog.com/blob/master/lib/changelog/regexp.ex#L2), but I just check that there's some stuff, and then an `@`, and then some stuff. 

```elixir
~r/^\S+@\S+\.\S+$/
```

That's pretty much what you're gonna be able to do... And that's just to basically make sure that you don't get some junk into your database... 🙅‍♀️

But still, all you've gotta do is send them an email, and if they click on it, well that's a valid email address. If they don't click on it, then who cares...? That's a hard-learned lesson! 

<mark>If you want to validate an email address, send it an email. Problem solved.</mark>

Until bots start clicking on emails. Then we're gonna have a whole new issue... But so far I don't think there are bots that will 

1. create a fake email address
2. sign up for your thing, and then
3. access that email address and click on the link

When we get there, then we'll have to come up with something else. But until then, just send it an email.

---

_What you've just read is an excerpt from [JS Party #39](https://changelog.com/jsparty/39). I fixed up the formatting a bit for readability, but these (almost) exact words were spoken by me during [the Pro Tips segment](https://changelog.com/jsparty/39#transcript-98) of that episode. In addition to tips like this one, we also [discuss news & trends](https://changelog.com/jsparty/77), interview [awesome guests](https://changelog.com/jsparty/71), teach each other things [like we're 5](https://changelog.com/jsparty/76), and have lots of fun doing it. You should totally come party with us [live](https://changelog.com/live) on Thursdays or [subscribe](https://changelog.com/jsparty) to the produced version! Take a listen and let us know what you think._ 💚

{% podcast https://dev.to/jsparty/39-experimenting-with-some-new-ideas- %}