Email Verification  in Javascript

Email Verification in Javascript

// Email Varification

// Input an Email

// Length of the email > 11

// after . only 2 or 3 characters allowed

// minimum 3 characters between @ and .

const email = prompt("Enter the email address");

const emailLen = email.length;

const dotIndex = email.lastIndexOf(".");

const atIndex = email.lastIndexOf("@");

const lastIndex = emailLen-1;

if( emailLen < 11 || 
    lastIndex - dotIndex < 2 || 
    lastIndex - dotIndex > 3 || 
    dotIndex - atIndex < 3)
    { 
       console.log("Indalid Email") 
    }

Problem statement

You have already completed the email validation program using "||" operator. Now modify the code and rewrite the conditions to validate the email using the "&&" operator.

Additionally, include one more condition that:

a- Email should have at least 3 characters before “@.” b-If the email is valid then store the value of email in the result variable with a message. Expected Input xyz@gmail.com Expected Output xyz@gmail.com Expected Input xz@gmail.com is valid Expected Output invalid email Test Cases Dot should be there before the alphabet

Check for Valid email

Check for invalid email

Check for the size of the email

Check for @ symbol

Check for the length after the dot

function main(email){
    let result;

    const emailLen = email.length;
    const dotIndex = email.lastIndexOf(".");
    const atIndex = email.lastIndexOf("@");
    const lastIndex = emailLen - 1;

    const hasAtLeast3BeforeAt = atIndex >= 3; // Condition a
    const hasDotBeforeAlphabet = dotIndex > 1 && 
                                 dotIndex < lastIndex; // Condition for dot placement

    const isValidEmail = emailLen >= 5 && atIndex !== -1 && 
                         dotIndex !== -1 &&
                         lastIndex - dotIndex >= 2 && 
                         lastIndex - dotIndex <= 4 &&
                         dotIndex - atIndex >= 2 && 
                         hasAtLeast3BeforeAt && 
                         hasDotBeforeAlphabet;

    if (isValidEmail) {
        result = email + " is valid";
    } else {
        result = "invalid email";
    }

    return result;
}