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

NCERT Class X Mathematics Chapter 1: Real Numbers

Chapter 1: Real Numbers Comprehensive Study Notes, Day-by-Day Explanations, and Question Bank Part 1: Day-to-Day Study Notes ...