我想验证包含多封电子邮件的此文本字段email
;全部由半冒号分隔,但这不起作用。当msn.com与其他电子邮件一起输入时,它应该会中断。
var isMsn = ["msn.com"];
function myFunction() {
var str = email.value;
//var isMsn = str.includes("msn.com");
var res = str.split(";");
var isValid = false;
//alert(res.length);
for (var i = 0; i < res.length; i++){
//alert(res[i]);
var x = res[i];
//check to see if this is an valid emAIL then
var mailformat = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
if(x.match(mailformat)){
alert("valid email address!");
//IF isMsn = TRUE then check to see if is contains msn.com
if (x.contains(isMsn) ){
alert("it is MSN");
//res[+1];
isValid = true;
}
else {
alert('no MSN');
if (res.includes(isMsn)){
//alert('Cannot mix MSN emails with other');
//isValid = false;
//res[-1];
break;
}
//else{
//alert('it is other');
//isValid = true;
//}
}
}
else{
alert("You have entered an invalid email address!");
isValid = false;
}
}
}
我希望它循环播放,所有电子邮件必须与msn.com或其他NOT两者匹配。例如,user @ msn.com; user1@msn.com或user@hotmail.com; user1@hotmail.com但不是user@msn.com; user1@hotmail.com
不确定.includes是否是最好的选择!
答案 0 :(得分:1)
1)您应该使用includes
代替contains
;
2)includes
或contains
方法接受字符串,而不是数组,因此它应该是res.includes(isMsn[0])
;
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes
如果您想要通过多个域进行验证,则不应将其命名为isMsn
,而应将prohibitedDomains
命名为Array.some
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some)。< / p>
prohibitedDomains.some((domain) => res.includes(domain))