Back to all snippets
Library/Utilities/Email Validation
javascriptbeginnervalidationemailregex

How to implement Email Validation in Javascript

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.

Code Snippet

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};

What is Email Validation?

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.

How It Works

  1. 1The simple regex checks for the presence of non-whitespace characters on both sides of `@` and after a dot.
  2. 2The strict regex enforces RFC-compliant local part characters (`a-z`, `0-9`, `._%-+`) and a TLD of at least 2 characters.
  3. 3Both return a boolean via `.test(email)` — no parsing needed.
  4. 4Always trim whitespace from the input before testing.

Common Use Cases

  • Sign-up forms - Validate email before submitting to the server
  • Newsletter forms - Check email format before adding to a mailing list
  • Contact forms - Ensure email field contains a valid address
  • Admin inputs - Validate emails entered in back-office tools

Key Benefits

  • Instant client-side feedback without a network request
  • Two versions: quick simple check or stricter RFC-aligned check
  • No external validation library needed
  • Easy to extend with additional rules

Common Mistakes to Avoid

  • Relying only on client-side regex — always validate on the server too; client-side checks can be bypassed.
  • Rejecting valid emails — addresses like `user+tag@subdomain.co.uk` are valid but fail many overly strict regexes.
  • Not trimming the input — a leading space will fail the check even for a valid address.

Quick Tips

  • Click the "Copy" button above to copy the code to your clipboard
  • This code is production-ready and can be used in your projects immediately
  • Check out related snippets below for more javascript examples

Frequently Asked Questions

Is regex email validation reliable?

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.

About This Javascript Code Snippet

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.

Tags: validation, email, regex  | Language: javascript  | Difficulty: beginner  | Category: Utilities

Build Your Own Snippet Library

Organise your team's code snippets with Snippetly. Share knowledge and boost productivity across your organisation.