'Check email domain type (personal email or company email)

Example

isPersonalEmail("[email protected]") // true
isPersonalEmail("[email protected]") // false

I can't find NPM package do that I need to check email in node.js server



Solution 1:[1]

I suggest to first check if the certain email is valid (like this) and after that you check against some given domains by yourself if it is a company email or not.

function isPersonalEmail(email, companyDomains) {
    if(!validateEmail(email)) {
        return false
    }
    // ensure email is not in companyDomains !
    return companyDomains.every(d => email.indexOf(`@${d}`) === -1)
}
function validateEmail(email) {
  return email.match(
    /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
  );
};

const email = '[email protected]'
const email2 = '[email protected]'
const email3 = 'no-email-whatsoever'
const email4 = '[email protected]'

// provide the companydomains. 
const companyDomains = ['companyName.com']
console.log(isPersonalEmail(email, companyDomains))  // true (personal email)
console.log(isPersonalEmail(email2, companyDomains)) // false  (company email!)
console.log(isPersonalEmail(email3, companyDomains)) // false  (invalid email)
console.log(isPersonalEmail(email4, companyDomains)) // true   (personal email!)

List of free email providers

For being able to filter by ALL FREE email providers you will have to host a list. After searching the web I found this list which may be a starting point for you, Ideally you would monitor what emails are accepted and which not and then update the list regularly by hand!

I honestly have no clue if this list is useful or not. Use at your own risk! .

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