Explore Topics

JavaScript : Regular Expression Email Validation

Last Updated : 18 Apr, 2025 - Asked By Ashok

email validation  regular expression 

Answers
2023-11-21 12:24:52

Here’s a regular expression for email validation using JavaScript:

function validateEmail(email) {
  var reg = /^ [A-Z0-9._%+-]+@ ([A-Z0-9-]+.)+ [A-Z] {2,4}$/i;
  if (reg.test(email)) {
    return true;
  }
  return false;
}
This regular expression checks if the email address is valid or not. It checks for the valid characters in the email ID (like, numbers, alphabets, few special characters). The regular expression pattern for email validation in JavaScript is /^ [a-zA-Z0-9._-]+@ [a-zA-Z0-9.-]+. [a-zA-Z] {2,4}$/. You can use this pattern to validate email address in your java script code.

2024-01-01 11:28:09

javascript
function validateEmail(email) {
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return emailRegex.test(email);
}


Explanation of the regular expression ^[^\s@]+@[^\s@]+\.[^\s@]+$:
- ^ asserts the start of the string.
- [^\s@]+ matches one or more characters that are not whitespace or '@'.
- @ matches the literal '@' character.
- [^\s@]+ matches one or more characters that are not whitespace or '@'.
- \. matches the literal '.' character (escaping it with a backslash).
- [^\s@]+ matches one or more characters that are not whitespace or '@'.
- $ asserts the end of the string.

To use this regular expression for email validation, call the validateEmail() function and pass the email address as an argument. For example:

javascript
const email = "example@example.com";
if (validateEmail(email)) {
    console.log("Valid email address");
} else {
    console.log("Invalid email address");
}


This function will return true if the email address matches the pattern specified in the regular expression and false otherwise. You can customize or modify the regular expression pattern based on specific requirements or additional email validation criteria.

Your Answer



Other Resources

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

Q&A Image
Q&A

Ask your coding-related doubts and get answers.

Certification Image
Certification

Earn certifications to enhance your resume.

internships Image
Internships

Hands-on projects to improve your skills.

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

blog Image
Blogs

Add your technical blogs and read technical topics.

Certification Image
Certification

Earn certifications to enhance your resume.

Q&A Image
Q&A

Hands-on projects to improve your skills.

People Also Asked