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

SEBA English Questions and Answer Bank of H.S.L.C.QUESTIONS From 2011 to 2020

  Answer Bank of H.S.L.C.QUESTIONS From 2011 to 2020   PROSE SECTION:   (Section-A)   1. A Letter to God G.L. Fuentes   ...