Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to Validate Email Address using RegExp in JavaScript?
Next article icon

How to Validate Email Address using RegExp in JavaScript?

Last Updated : 15 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Validating an email address in JavaScript is essential for ensuring that users input a correctly formatted email. Regular expressions (RegExp) provide an effective and efficient way to check the email format.

Why Validate Email Addresses?

Validating email addresses is important for a number of reasons:

  • User Input: Ensure that users enter a correctly formatted email.
  • Avoid Errors: Prevent errors caused by invalid email formats that might break functionality.
  • User Experience: Provide real-time feedback to users about their email input, improving the overall experience.
  • Security: Validating the email helps in preventing malicious input, reducing the risk of injection attacks.

What is a Regular Expression?

A Regular Expression (RegExp) is a sequence of characters that forms a search pattern. In JavaScript, RegExp objects are used for pattern matching within strings, such as checking the format of email addresses. For email validation, we use RegExp to check if an email address follows the general structure of a valid email.

A common RegExp pattern to validate email addresses looks like this:

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

Regex Breakdown:

  • ^[a-zA-Z0-9._%+-]+: Matches the username part of the email, allowing alphanumeric characters and some special characters like ., _, %, +, and -.
  • @: Matches the literal "@" symbol that separates the username from the domain.
  • [a-zA-Z0-9.-]+: Matches the domain part, allowing letters, digits, dots, and hyphens.
  • \.: Escapes the dot (.) to match the literal period separating the domain from the top-level domain (TLD).
  • [a-zA-Z]{2,}$: Matches the top-level domain (TLD), which must consist of at least 2 alphabetic characters.

Validating Email Address Format in JavaScript Regex

You can use either the test() method or the match() method with a regular expression to validate an email.

1. Using the test() Method with RegExp

You can use either the test() method or the match() method with the RegExp pattern to validate the email format.

JavaScript
let regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; let mail = "[email protected]"; if (regex.test(mail)) {     console.log("Valid Email address"); } else {     console.log("Invalid Email address"); }  
  • regex.test(mail) checks if the email matches the regular expression.
  • If the email matches the pattern, it prints "Valid Email address."
  • Otherwise, it prints "Invalid Email address."

2. Using match() with RegExp

Another approach is to use the match() method, which returns an array if the email matches the regular expression or null if it doesn’t.

JavaScript
//Driver Code Starts let regex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/; let mail = "[email protected]"; //Driver Code Ends  let isValid = mail.match(regex); if (isValid) {     console.log("Valid email address"); } else {     console.log("Invalid email address"); }  
  • mail.match(regex) tries to match the email against the regular expression.
  • If there is a match, it returns an array with the matched string. If no match is found, it returns null.
  • In this example, the result is stored in isValid and used to check if the email is valid.

Conclusion

Using RegExp in JavaScript is an efficient way to validate email addresses by checking the structure of the email. This ensures the email includes a valid username, the "@" symbol, a valid domain, and a correct TLD (like .com or .org). However, keep in mind that this method only checks the format and doesn't confirm whether the email address is deliverable or belongs to an active account.

  • Use the test() method for a simple true/false check if you only need to know whether the email is valid.
  • Use the match() method if you want more detailed information about the match, like extracting parts of the email or needing the match result for further processing.



Next Article
How to Validate Email Address using RegExp in JavaScript?

P

PranchalKatiyar
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-RegExp

Similar Reads

    How to Validate Email Address without using Regular Expression in JavaScript ?
    Email validation in JavaScript is the process of ensuring that an email address entered by the user is in the correct format and is a valid email address or not. This is typically done on the client side using JavaScript before the form is submitted to the server.An email address must have the follo
    5 min read
    JavaScript - How to Validate Form Using Regular Expression?
    To validate a form in JavaScript, you can use Regular Expressions (RegExp) to ensure that user input follows the correct format. In this article, we'll explore how to validate common form fields such as email, phone number, and password using RegExp patterns.1. Validating an Email AddressOne of the
    4 min read
    How to Validate Decimal Numbers in JavaScript ?
    Validating user input is an essential aspect of Web Development. As a developer, when we are playing with the numeric inputs provided by the end-user, it is quite important to ensure that the input provided by the user is in the correct format. We can use the regular expression to Validate Decimal N
    2 min read
    How to Validate an Input is Alphanumeric or not using JavaScript?
    To validate alphanumeric in JavaScript, regular expressions can be used to check if an input contains only letters and numbers. This ensures that the input is free from special characters.Approach: Using RegExpA RegExp is used to validate the input.RegExp is used to check the string of invalid chara
    1 min read
    JavaScript regex - Validate Credit Card in JS
    To validate a credit card number in JavaScript we will use regular expression combined with Luhn's algorithm. Appling Luhn's algorithm to perform a checksum validation for added securityLuhn algorithm:It first sanitizes the input by removing any non-digit characters (e.g., spaces).It then processes
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences