Email Validation with JavaScript

Learn how to validate email addresses using JavaScript - from basic checks to advanced regular expressions.

Introduction to Email Validation

Email validation is crucial for web forms to ensure users provide properly formatted email addresses. JavaScript provides several techniques to validate emails on the client side before submitting data to the server.

Proper email validation helps reduce errors, prevent spam, and improve user experience by providing immediate feedback. While client-side validation is convenient, remember that server-side validation is still essential for security.

Key aspects of email validation:
  • Basic syntax checking (@ and domain presence)
  • Advanced pattern matching with regular expressions
  • Real-time validation as users type
  • Clear feedback for valid and invalid inputs
  • Custom validation rules based on specific requirements

Example 1: Basic Email Validation

This example demonstrates the most fundamental email validation - checking for the presence of "@" symbol and a domain extension. While simple, it catches the most obvious mistakes.

// Basic Email Validation
function validateBasicEmail(email) {
    if (!email) return false;
    
    // Check for @ symbol
    const atIndex = email.indexOf('@');
    if (atIndex < 1) return false;
    
    // Check for dot after @
    const dotIndex = email.indexOf('.', atIndex + 1);
    if (dotIndex < atIndex + 2) return false;
    
    // Check domain extension (at least 2 characters)
    const domain = email.substring(dotIndex + 1);
    return domain.length >= 2;
}

// Usage example
const email = "[email protected]";
if (validateBasicEmail(email)) {
    console.log("Email is valid");
} else {
    console.log("Email is invalid");
}

Basic Email Validation Live Demo

Example 2: Advanced Email Validation

This example uses a comprehensive regular expression to validate email addresses according to standard email format specifications. It checks for valid characters, proper structure, and domain format.

// Advanced Email Validation with Regular Expression
function validateAdvancedEmail(email) {
    // Comprehensive email regex pattern
    const regex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
    return regex.test(email);
}

// Usage example
const email = "[email protected]";
if (validateAdvancedEmail(email)) {
    console.log("Email is valid");
} else {
    console.log("Email is invalid");
}

Advanced Email Validation Live Demo

Example 3: Real-time Email Validation

This example demonstrates real-time validation as the user types. It provides immediate feedback using color-coded indicators, helping users correct mistakes before form submission.

// Real-time Email Validation
const emailInput = document.getElementById('realtimeEmail');
const realtimeResult = document.getElementById('realtimeResult');

emailInput.addEventListener('input', function() {
    const email = this.value;
    
    if (!email) {
        realtimeResult.textContent = '';
        realtimeResult.className = 'validation-result';
        return;
    }
    
    if (validateAdvancedEmail(email)) {
        realtimeResult.textContent = '✓ Valid email format';
        realtimeResult.className = 'validation-result valid';
    } else {
        realtimeResult.textContent = '✗ Invalid email format';
        realtimeResult.className = 'validation-result invalid';
    }
});

Real-time Email Validation Live Demo

More Validation Examples

Explore more JavaScript validation examples to enhance your web forms:

Password Strength

Validate and measure password strength

View Example

Credit Card Validation

Validate credit card numbers with Luhn algorithm

View Example

URL Validation

Validate website URLs with JavaScript

View Example

Phone Number Validation

Validate international phone numbers

View Example