Redirect Image

js

Friday, December 8, 2023

Write JavaScript using regular expression to check if an email satisfies all the required conditions.

Q. "It is important to validate the email before you can store or process it". Write JavaScript using regular expression to check if an email satisfies all the required conditions.

Answer:

function isValidEmail(email) {
    // Regular expression for basic email validation
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

    // Test the email against the regular expression
    return emailRegex.test(email);
}

// Example usage:
const emailToValidate = 'user@example.com';
if (isValidEmail(emailToValidate)) {
    console.log('Valid email address!');
} else {
    console.log('Invalid email address.');
}
 

No comments:

Post a Comment

ASSEB Class IX English Textbook:Moments, Chapter 2: The Adventures of Toto Additional Questions and Answers

Chapter: The Adventures of Toto Addtional Questions and Answers Multiple Choice Questions (MCQs) 1. Who is the author of "The...