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
console.log(validateEmail('info123@example.com'));
Console Output 1:
console.log(validateEmail('abc-defmail.com'));
Console Output 2:
Solution:
function validateEmail(email) {
//fill the code
var regx=/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
if(email.match(regx)){
return "Valid email address!"
}
else{
return "Invalid email address!"
}
}
console.log(validateEmail('abc-defmail.com'));