javascript - jQuery 条件传递

标签 javascript jquery

我为 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 为 truephone2, 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/

相关文章:

javascript - 定位元素以覆盖跨度

javascript - ASP.NET MVC 数据注释正则表达式在 .NET 中有效,但在 JS 中无效

javascript - 进行页面刷新时防止显示未设置样式的页面

javascript - 创建固定的 div 高度

javascript - 更改 Ajax Post 参数并根据交替依赖的下拉列表返回 HTML

jQuery 提交功能无法正常工作

javascript - jquery动态创建数组

javascript - 纯js中如何在变量名中使用多变量字符串

javascript - 有没有办法为 Jasmine 单元测试(Angular 4)提供基类?

从弹出窗口读取javascript错误