我为 2 个输入做了自己的表单验证,一个用于电话号码,另一个用于电子邮件地址。而且我在 1 页中有 2 个表格。
我的代码是
var email, phone;
if (email address validation passed) {
email = true;
} else {
email = false;
}
if (phone number validation passed) {
phone = true;
} else {
phone = false;
}
if (!(phone && email)) {
return false
} else {
return true
}
因为我在同一个页面上有两个表单,所以我想为第二个表单添加另一个片段,例如,
var email2, phone2;
if (email address validation passed) {
email2 = true;
} else {
email2 = false;
}
if (phone number validation passed) {
phone2 = true;
} else {
phone2 = false;
}
if (!(phone2 && email2)) {
return false
} else {
return true
}
我发现的问题是,为了提交表单,我需要让 email, phone, email2, phone2;
都等于 true。但是,如果 email, phone 为 true
或 phone2, email2 为 true
,我需要提交
只需要有人检查这是否是解决我的问题的正确逻辑方法?
if (!(phone2 && email2)) {
return false
} else if(!(phone && email )) {
return false
} else return true;
最佳答案
however, I need to submit in condition if email, phone are true or phone2, email2 are true
您所说的方式是最简单的编码方式:
if ((email && phone) || (email2 && phone2)) {
return true;
} else {
return false;
}
如果您只想根据条件是真还是假返回真或假,您可以像这样在一行中完成:
return (email && phone) || (email2 && phone2);
关于javascript - jQuery 条件传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16188009/