gpt4 book ai didi

javascript - 必须至少完成两个字段之一的验证

转载 作者:行者123 更新时间:2023-12-03 12:03:02 25 4
gpt4 key购买 nike

首先,我使用jquery验证插件。

我在 JavaScript 中进行了以下验证:

var m = mob.substr(0,2);
var l = land.substr(0,2);

if (mob == '' && land == '') {
alert("You must enter at least one phone number");
return false;
}
else if (mob.length != 10 || m != "04") {
alert("Mobile number is invalid. Must be a valid australian mobile number.");
return false;
}
else if (land.length != 10 || l == "04" || l == "01" || l == "05" || l == "06" || l == "09" || l == "00") {
alert("Landline is invalid. Must be a valid australian landline number.");
return false;
}

正如标题所示,我希望用户能够仅输入一个或两个字段,但不能将两个字段都留空。

如何将其纳入验证中?

最佳答案

var m = mob.substr(0,2);
var l = land.substr(0,2);

if (mob === '' && land === '') {
alert("You must enter at least one phone number");
return false;
}
if (mob !== '' && (mob.length !== 10 || m !== "04")) {
alert("Mobile number is invalid. Must be a valid australian mobile number.");
return false;
}
if (land !== '' && (land.length !== 10 || l === "04" || l === "01" || l === "05" || l === "06" || l === "09" || l === "00")) {
alert("Landline is invalid. Must be a valid australian landline number.");
return false;
}
return true;

这里只需在最后两个条件的基础上再添加一个条件即可。并使它们成为独立的 if 语句。所以他们都会被评估。

关于javascript - 必须至少完成两个字段之一的验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25320256/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com