Featured Article

Master JavaScript Email Validation Regex w3schools Guide

Adam Jul 13, 2026

Email validation is a crucial aspect of web development, ensuring that users enter valid email addresses when signing up for services, communicating with support, or filling out contact forms. JavaScript, along with regular expressions (regex), can be used to accomplish this. W3Schools, an industry-standard resource for web developers, provides a comprehensive guide on regex, making it an excellent starting point for email validation.

How To Validate Email Address In JavaScript With Regular Expression (3 Simple Guide)
How To Validate Email Address In JavaScript With Regular Expression (3 Simple Guide)

Email validation regex can vary in complexity, depending on how strictly you want to validate the inputs. A simple regex pattern can check for the basic structure of an email address, while a more complex one can adhere to the official RFC 5322 standards.

Advanced Form Validation In JavaScript
Advanced Form Validation In JavaScript

Basic Email Validation with JavaScript Regex

W3Schools suggests using the following regex pattern for a simple email validation:

Email validation in HTML πŸ‘‡
Email validation in HTML πŸ‘‡

/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/

Let's break down this regex:

Registration Form In HTML With Javascript Validation With Source Code
Registration Form In HTML With Javascript Validation With Source Code
  1. ^[a-zA-Z0-9._%+-]+ - Start of the string and one or more of the following characters: letters, numbers, periods, underscores, percent signs, plus signs, or hyphens.
  2. @ - A single @ symbol.
  3. [a-zA-Z0-9.-]+ - One or more of the following characters: letters, numbers, periods, or hyphens.
  4. . - A period.
  5. [a-zA-Z]{2,}$ - Two or more letters at the end of the string.

Using the Regex in JavaScript

Here's a simple JavaScript function using the provided regex to validate an email:

Easy Form Validation Using Regex
Easy Form Validation Using Regex

function validateEmail(email) {
  const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
  return emailRegex.test(email);
}

Handling Invalid Emails

In practice, you might want to handle invalid emails differently, perhaps by showing an error message or providing immediate feedback to the user. Here's an example using HTML and JavaScript:

<input type="email" id="email" pattern="[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}" required>
<span id="message"></span>

<script>
  const input = document.getElementById('email');
  const message = document.getElementById('message');

  input.addEventListener('input', function(e) {
    if (!input.validity.valid) {
      message.textContent = 'Please enter a valid email address.';
    } else {
      message.textContent = '';
    }
  });
</script>

Advanced Email Validation using RFC 5322 Standards

Javascript Fundamental
Javascript Fundamental

For more robust validation, consider using a regex pattern that follows the official RFC 5322 standards for email addresses. This can be complex and lengthy, but libraries like the one maintained by email-validator can help simplify the process.

Using a Library for RFC 5322 Validation

Email Address Input Validation
Email Address Input Validation
JavaScript Form Events: Handle User Input Like a Pro πŸ“
JavaScript Form Events: Handle User Input Like a Pro πŸ“
JavaScript Full Notes Every Beginner Must Save πŸ’‘
JavaScript Full Notes Every Beginner Must Save πŸ’‘
Newsletter Signup Form HTML CSS JavaScript | Animated With Validation
Newsletter Signup Form HTML CSS JavaScript | Animated With Validation
HTML5 Form Validation With Regex
HTML5 Form Validation With Regex
JavaScript Fundamental
JavaScript Fundamental
Javascript window.confirm() method | Javascript tutorial
Javascript window.confirm() method | Javascript tutorial
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Basics You MUST Know
JavaScript Basics You MUST Know
Javascript Cheet cheet
Javascript Cheet cheet
JavaScript Reserved Words: What You Need to Know πŸš€
JavaScript Reserved Words: What You Need to Know πŸš€
an image of a web page with different types of text
an image of a web page with different types of text
an image of a blue screen with text on it that reads,'create code box for
an image of a blue screen with text on it that reads,'create code box for
JavaScript study guide for fast learning
JavaScript study guide for fast learning
Declaring a variable in JavaScript using the let keyword
Declaring a variable in JavaScript using the let keyword
Read Emails Automatically with Python – imaplib & Gmail IMAP Tutorial
Read Emails Automatically with Python – imaplib & Gmail IMAP Tutorial
JavaScript Projects That Will Make You Better Fast 😳
JavaScript Projects That Will Make You Better Fast 😳
Synchronous & Asynchronous Javascript
Synchronous & Asynchronous Javascript
Master JavaScript Variables: A Beginner’s Guide πŸš€
Master JavaScript Variables: A Beginner’s Guide πŸš€
Learn JavaScript in 30 Days
Learn JavaScript in 30 Days

The email-validator library provides a simple, straightforward API for validating email addresses according to the RFC 5322 standards. Here's an example using Node.js:

const EmailValidator = require('email-validator');

const validator = new EmailValidator();

const email = 'test@example.com';
const isValid = validator.validate(email);

console.log(isValid ? 'Valid' : 'Invalid');

In conclusion, email validation is a crucial component of web development, and JavaScript with regex provides a powerful way to accomplish this. W3Schools offers a great starting point for understanding and implementing regex for email validation. However, for more advanced validation, consider using libraries that adhere to the official RFC 5322 standards. Always strive to provide the best user experience possible, with clear and immediate feedback for invalid inputs.