Validate email addresses with regex
Quick Answer
Use `/^[^\s@]+@[^\s@]+\.[^\s@]+$/` for a quick format check, or the stricter RFC-aligned pattern for production form validation.
1const isValidEmail = (email) => {
2 const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
3 return emailRegex.test(email);
4};
5
6// More strict validation
7const isValidEmailStrict = (email) => {
8 const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
9 return emailRegex.test(email);
10};Email validation with regex is a first line of defence for form inputs. The simple pattern catches the most obvious invalid formats. The strict pattern enforces standard RFC-compliant characters. Both are intentionally lenient for the domain part since TLDs like .academy or .photography are valid but would be rejected by overly strict patterns.
Regex can only check the format, not whether the address exists. For reliable verification, send a confirmation email and require the user to click a link.
This free javascript code snippet for email validation is production-ready and copy-paste friendly. Whether you are building a web app, API, or frontend interface, this beginner-level example will help you implement email validation quickly and correctly.
All snippets in the Snippetly library follow javascript best practices and are tested for real-world use. You can adapt this code to work with React, Vue, Node.js, or any project that uses javascript.
Organise your team's code snippets with Snippetly. Share knowledge and boost productivity across your organisation.