简体   繁体   English

如何在 JavaScript 中验证仅工作 Email?

[英]How to validate only working Email in JavaScript?

How to validate only working Email in JavaScript?如何在 JavaScript 中验证仅工作 Email? Actually, I want only working email excluding (@gmail.com, @outlook.com, @hotmail.com, @yahoo.com etc).实际上,我只想工作 email,不包括(@gmail.com、@outlook.com、@hotmail.com、@yahoo.com 等)。 I want only working emails like abc@stack.com etc.我只想要工作电子邮件,如 abc@stack.com 等。

This code is also working!此代码也有效!

 let text = "abc@hotmail.com"; 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") }

One way is to get the domain part from the email, and check it against the list of personal email domains:一种方法是从 email 中获取域部分,并对照个人 email 域列表进行检查:

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

console.log(workingEmailValidator('xyz@sss.com'))

I use my own function after checking if email is valid then you can check if it is work email or not:我在检查 email 是否有效后使用我自己的 function 然后你可以检查它是否工作 email 或不:

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("abc@paiman.com");
check("abc@gmail.com.com");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM