Hacker News new | ask | show | jobs
by jedberg 475 days ago
> I have a very long regular expression (email validation of course)

On a tangentially related note, I guarantee you that your regex is wrong. There is only one way to validate an email address:

Send an email to it and have them respond. Otherwise you will block some valid users.

Now of course you can make a regex that gets most email addresses, and if you're ok with that, then that's fine. But if you don't want to accidentally exclude someone, then sending email is the only way to validate it.

1 comments

It's very easy to make a regex that allows all, but catches simple errors: /.+@.+/. Maybe narrow down the domain name, but don't forget that trailing dots are valid in DNS names.

Why is everyone trying to check for things they don't have to? If you need a valid email address, of course you have to send an email for confirmation. anna@example.com is perfectly syntactically valid, but isn't useful to anyone for sending emails. If you optionally want your users to enter an email address, don't overcomplicate things.

> Why is everyone trying to check for things they don't have to?

I forgot where I read it (maybe something about testing or DDD), but an idea I like much is to not validate stuff coming from an external system other than for your internal constraints. You don't control an email account and how it was created and the specification is messy, so if you want to check for its existence, you query the other system. Same for other identifiers.