Regular Expression

This approach involves defining a regular expression pattern that matches valid email formats. We can then use the test() method of the regular expression object to check if an input string matches the pattern.

Syntax:

const emailPattern = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
const isValid = emailPattern.test(email);

Javascript




const email = "nikhil@gmail.com";
const emailPattern = 
    /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
const isValid = emailPattern.test(email);
console.log(isValid);


Output

true

JavaScript Program to Validate An Email Address

In this article, we will see how to Validate An Email Address in JavaScript. Validating an email address is a common task in web development. It involves ensuring that an email address entered by a user conforms to a valid format.

Table of Content

  • Regular Expression
  • Using Library Validator

Similar Reads

Approach 1: Regular Expression

This approach involves defining a regular expression pattern that matches valid email formats. We can then use the test() method of the regular expression object to check if an input string matches the pattern....

Approach 2: Using Library Validator

...