Email ID validation using JavaScript

Email ID validation in JavaScript can be achieved using regular expressions(regex) to check if the input follows the standard format of an email address. It is a common task in web development to ensure that users provide a valid email address before submitting a form.

How to verify Email ID validation using JavaScript

Here are the steps:

  1. Create a regular expression that matches the format of an email address. The regular expression should cover basic requirements such as the presence of “@” symbol and a domain with at least one dot.
  2. Implement a JavaScript function that takes an an email address as input and uses the regular expression to test its validity.
  3. Test the validation function with various email addresses to ensure it correctly identifies valid and invalid email formats.
function validateEmail(email)
{
const regex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return regex.test (email);
}
const email1 = "[email protected]";
const email2 = "apple-email";
console.log('Email: ${email1} is ${validateEmail(email1) ? 'Valid' : 'Invalid'});
console.log('Email: ${email2} is ${validateEmail(email2) ? 'Valid' : 'Invalid'});

OUTPUT:

Email: [email protected] is Valid
Email: apple-email is Invalid

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top