How to Validate Email Address using RegExp in JavaScript?
Last Updated :
15 Apr, 2025
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.
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
How to check a date is valid or not using JavaScript? To check if a date is valid or not in JavaScript, we have to know all the valid formats of the date. For ex - "YYYY/DD/MM", "DD/MM/YYYY", and "YYYY-MM-DD", etc. We have a given date format and we need to check whether the given format is valid or not according to the official and acceptable date for
3 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
How to check for IP address using regular expression in javascript? The task is to validate the IP address of both IPv4 as well as IPv6. Here we are going to use RegExp to solve the problem. Approach 1: RegExp: Which split the IP address on. (dot) and check for each element whether they are valid or not(0-255). Example 1: This example uses the approach discussed abo
2 min read
Validate a password using HTML and JavaScript Validating a password using HTML and JavaScript involves ensuring that user-entered passwords meet certain criteria, such as length, complexity, or character types (e.g., uppercase, lowercase, numbers, and symbols). This process enhances security by enforcing strong password requirements before form
2 min read
How to Validate an Email in ReactJS ? Validating email in React is an important step to authenticate user email. It ensures the properly formatted email input from the user. The following example shows how to validate the user entered email and checking whether it is valid or not using the npm module in React Application.ApproachTo vali
2 min read
JavaScript - Check if a String is a Valid IP Address Format An IP address is a unique identifier assigned to each device connected to a computer network that uses the Internet Protocol for communication. There are two common types of IP addresses: IPv4 and IPv6. In this article, weâll explore how to check if a string is a valid IP address format in JavaScrip
2 min read