如何验证只在JavaScript中工作的电子邮件



如何在JavaScript中验证只能工作的电子邮件?事实上,我只想要工作电子邮件,不包括(@gmail.com、@outlook.com、@hotmail.com、@yahoo.com等(。我只想要像这样的工作电子邮件abc@stack.com等

这段代码也在工作!

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")
}

一种方法是从电子邮件中获取域部分,并根据个人电子邮件域列表进行检查:

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

我在检查电子邮件是否有效后使用自己的功能,然后您可以检查它是否是工作电子邮件:

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");

最新更新