Validate Email Address

medium

By - Aman Pareek

Last Updated - 15/09/2024

Problem Statement

Alia is working on a web application that requires users to input their email addresses. To ensure that these email addresses are correctly formatted and likely valid, she needs a function that can accurately determine if an email address is valid according to common standards.

Requirements

  1. Input: A string representing an email address.

  2. Output: A boolean value indicating whether the email address is valid

Email Address Validity Criteria:

  • The email address must follow the basic structure of localpart@domain.tld.

  • The localpart (the part before @) can contain alphanumeric characters and some special characters like . (dot), _ (underscore), % (percent), + (plus), and - (hyphen).

  • The domain (the part after @) can include alphanumeric characters, dots, and hyphens.

  • The tld (top-level domain) must be at least two characters long and consist of alphabetic characters.

Example 1

Input: password = "example@example.com"

Output: true

Example 2

Input: password = "invalid-email"

Output: false

Example 3

Input: password = "user@domain.co"

Output: true

Example 4

Input: password = "user@domain.c"

Output: false

Solution 1: Email Address Validation using Regular Expressions

function isValidEmail(email) {
    // Regex pattern for email validation
    const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
    
    // Test the email against the regex pattern
    return emailRegex.test(email);
} 

const password1 = "example@example.com";
isValidEmail(password1);  //output: true 

const password2 = "invalid-email";
isValidEmail(password2);  //output: false 

const password3 = "user@domain.co";
isValidEmail(password3);  //output: true 

const password4 = "user@domain.c";
isValidEmail(password4);  //output: false 
  • Local Part: Must start with one or more alphanumeric characters or special characters (._%+-), followed by an @ symbol.

  • Domain Part: Follows the @ with one or more alphanumeric characters or dots and hyphens.

  • Top-Level Domain: Ends with a dot followed by at least two letters (e.g., .com or .org).

Resources

Link Logo
Validator.js

A NPM package that helps you test or validate your email address

Frequently asked questions

  1. Why email address validation is important?

    • Accurate Communication: Validating email addresses ensures that the emails you send reach the intended recipients. This is crucial for account verifications, password resets, and promotional communications.

    • Reduce Bounces: Proper validation helps in minimizing email bounce rates. Invalid or misspelled email addresses can lead to bounces, which can affect your sender reputation and email deliverability.

    • User Experience: Providing immediate feedback on invalid email formats enhances the user experience. It helps users correct mistakes before they submit a form.

    • Prevent Fraud: Validation can help prevent users from entering fake or disposable email addresses, which could be used for fraudulent activities.

    • Maintain Data Quality: Ensuring that email addresses are valid helps in maintaining the quality of your user database and improves the effectiveness of your marketing campaigns.

  2. Ways to validate Email address?

    There are so many ways as follows:

    • Using Libraries: Libraries like validator.js (JavaScript), email-validator (Python), or similar tools in other languages provide more robust validation, including syntax checking and sometimes domain verification.

    • Third-Party APIs: Services like Hunter.io, ZeroBounce, or NeverBounce offer APIs to verify the validity of email addresses, including checking whether the email is temporary or associated with spam traps.

    • Double Opt-In: Send a confirmation email to the user’s provided email address. This not only verifies that the email format is correct but also that the user has access to the inbox. This is often used for account verification or subscription confirmations.

    • You can implement specific rules based on your application’s needs, such as disallowing certain domains or checking for custom patterns.