4 answers

★ Best answer

The most important thing is to use `^[^\s@]+@[^\s@]+\.[^\s@]+$` for a basic check, because perfect regexes for emails are a nightmare and nobody actually uses them. If you need to be serious about it, rely on a dedicated library like `email-validator` instead of making up your own pattern.

It depends on what you need to do, really. If you just want a quick client-side check to give feedback to the user before submission, then a simple regex works. But if you think a regex will protect you from invalid addresses or that it's a real validator, you're wasting your time. The RFC specs for emails are so complicated that a perfect regex would be unreadable.

The best approach is always to send a confirmation email. Who cares if the format is technically valid according to RFC 5322 - if the person clicks the link you sent them, you know it works. If you absolutely need a pattern for the frontend, go with something reasonable like `^[^@\s]+@[^@\s]+\.[^@\s]+$`, but think of it more as theater than real validation. On the backend, at least check that the domain exists (MX record) if you really want to be serious about it, otherwise just rely on the confirmation email.

What you're missing is that you shouldn't even trust regex in production, really. The real check is sending a confirmation email and seeing if the user clicks the link - that's the only validation that matters. Regex is just for weeding out the most obvious typos on the client side, but a string like `foo@bar.co` is perfectly valid for a simple pattern and could still be a typo.

Don't chase the perfect regex - it's a classic trap. A simple pattern like `^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$` beats anything complicated because it actually works and stays readable, and anyway you should always send a confirmation email to make sure it's actually valid.

Your answer

Log into answer.