'How to validate only working Email in JavaScript?

How to validate only working Email in JavaScript? Actually, I want only working email excluding (@gmail.com, @outlook.com, @hotmail.com, @yahoo.com etc). I want only working emails like [email protected] etc.



Solution 1:[1]

One way is to get the domain part from the email, and check it against the list of personal email domains:

const workingEmailValidator = email => 
    !['gmail', 'hotmail', 'yahoo', 'outlook'].includes(email.split('@')[1].split('.')[0])

console.log(workingEmailValidator('[email protected]'))

Solution 2:[2]

This code is also working!

let text = "[email protected]";
let domain = text.substring(text.lastIndexOf("@"));
if(domain == "@gmail.com" || domain == "@yahoo.com" || domain == "@hotmail.com" || domain == "@outlook.com"){
    console.error("Wrong format")
}else{
    console.log("working email")
}

Solution 3:[3]

I use my own function after checking if email is valid then you can check if it is work email or not :

const notAllowed = ["gmail.com", "email.com", "yahoo.com", "outlook.com"];
 function check(email) {
    const lastPortion = email.split("@")[1].toLowerCase();
     if (notAllowed.includes(lastPortion)) {
          console.log("Please enter work email");
          return false;
     }
     return true;
}
check("[email protected]");
check("[email protected]");

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ThePyGuy
Solution 2 Abdul Sammad
Solution 3 Paiman Rasoli