Ads Here

Friday, September 3, 2021

Validate Email - Regular Expression & test Function/ JavaScript

 

Validate Email - Regular Expression & test Function

Write a JavaScript program to validate an email id using Regular Expression. 

The function validateEmail must take an email string as its argument and return one of the following 2 statements:
Valid email address! - when found valid &
Invalid email address! - when found invalid

Note: Refer to the function arguments within the given console.log to create a regex pattern for valid email address. You may verify the correctness of your function by invoking it from within console.log


ANSWER:

script.js

function validateEmail(email) {

   //fill the code

   if(/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/i.test(email)){

       return "Valid email address!";

   }

   else{

       return "Invalid email address!"

   }

}


No comments:

Post a Comment