DEV Community

Cover image for There's only one way to validate an email address
Jerod Santo
Jerod Santo

Posted on • Originally published at changelog.com

There's only one way to validate an email address

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 only way you can do it. I know what you're thinking,

"I have the best regular expression for this!"

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 - you have two problems. 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.)

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, but it says my email address isn't valid, and it obviously is valid, 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, but I just check that there's some stuff, and then an @, and then some stuff.

~r/^\S+@\S+\.\S+$/
Enter fullscreen mode Exit fullscreen mode

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!

If you want to validate an email address, send it an email. Problem solved.

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. I fixed up the formatting a bit for readability, but these (almost) exact words were spoken by me during the Pro Tips segment of that episode. In addition to tips like this one, we also discuss news & trends, interview awesome guests, teach each other things like we're 5, and have lots of fun doing it. You should totally come party with us live on Thursdays or subscribe to the produced version! Take a listen and let us know what you think. 💚

play pause JS Party

Latest comments (30)

Collapse
 
intrnl profile image
intrnl

Really hate it when some sites disallow the use of + in an email address even though it's completely valid.

Collapse
 
itr13 profile image
Mikael Klages • Edited

Tried to put it in at debuggex but it couldn't validate test@test.com, guessing it's one of those that also includes other information? Side-note the graph ended up so big that I couldn't see even half of it when fully zoomed out.

Collapse
 
christopherib profile image
Chris Black

I've always been curious, how do services like kickbox.com validate email addresses?

Collapse
 
jerodsanto profile image
Jerod Santo

Yeah... I'm not gonna touch that with a 10 foot pole 😉

Collapse
 
ferricoxide profile image
Thomas H Jones II • Edited

Thus the recent FCC warnings about dialing back unknown "local" numbers. Thus, "be careful about calling back".

A couple weeks ago, I received a call that, on my cell, very obviously came from the 20 country-code. However, on my VOIP line's handset - and its simplified display - the number simply appeared as a ten-digit 202NNNNNNN (the 202 area-code is local to me) number.

Collapse
 
michi profile image
Michael Z

In Japan many people use emails that do not follow the standard. For example .name..@example.com

Such emails were given out by one of the major phone providers if I remember correctly.

Collapse
 
jerodsanto profile image
Jerod Santo

Yet another edge case to break our regexen! 😭

Collapse
 
pbouillon profile image
Pierre Bouillon

What about "10MinutesMail" services and others like that ?

Collapse
 
ferricoxide profile image
Thomas H Jones II

Used to be, you could validate an email address by connecting to the SMTP server listed in the address's MX record, and then do a VRFY. But, also thanks to spammers, you can almost never do that any more.

Collapse
 
jerodsanto profile image
Jerod Santo

The good ole' days! 👴

Collapse
 
timkor profile image
Timkor • Edited

Interesting article. However, what do you actually propose that a valid email address is:

  • An emailadress that works? Then this check is great.
  • Detecting it's not a bot? I don't really see this working. Especially not on long term.
  • Detecting it's not fraud/scam? Does not work. Scammers can steal credentials of their victims or what happens more often, just let the victims confirm their email.

If it's just about checking if a user did not mistyped his of her email. You're best off using a well tested browser implementation or just keeping the input loosely validated. The last thing you want is loosing conversion by someone that can not enter a valid email. At the end, it's the users responsibility to enter their correct emailadress.

If you really want to be sure and do not want to depend on a browser implementation or a strict validation process. You could even ask their email twice. Then the chance that the user mistyped will be very, very low.

Collapse
 
itr13 profile image
Mikael Klages • Edited

Would a regex catch either of the last two cases?

Collapse
 
timkor profile image
Timkor

No, pretty impossible using regex. There are tools like Siftscience though.

Collapse
 
jerodsanto profile image
Jerod Santo

what do you actually propose that a valid email address is

To me, valid means it's an email address in control of a human who entered it in earnest. You could layer on bot defense (via recaptcha, etc) either on the email form itself or on the confirmation page linked to in the email. But in my opinion, that's a separate concern.

You're best off using a well tested browser implementation or just keeping the input loosely validated.

I'm all for using <input type=email> and letting browsers do their thing. But that's more for UX than it is for me as the site owner.

Collapse
 
ferricoxide profile image
Thomas H Jones II

If you really want to be sure and do not want to depend on a browser implementation or a strict validation process. You could even ask their email twice. Then the chance that the user mistyped will be very, very low.

Though, if you do that, you probably want to disable the ability to paste from cut-buffer into the form. Otherwise, most people will just copy-paystah and you can end up with the wrong string twice.

Remember: there's people out there constantly trying to build a better idiot.

Collapse
 
matthewbdaly profile image
Matthew Daly

I agree that you shouldn't use a regex, but PHP in particular has the filter_var() function, which is a far better option. There are a few edge cases that are validated incorrectly, but it's generally fairly reliable.

However, sending an activation email is probably prudent in most cases since just because an email address is valid doesn't mean it actually exists.

Collapse
 
ferricoxide profile image
Thomas H Jones II

However, sending an activation email is probably prudent in most cases since just because an email address is valid doesn't mean it actually exists.

Or is owned by the person submitting the address… :p

Collapse
 
jerodsanto profile image
Jerod Santo

💯