douyi6290 2013-07-24 05:26
浏览 120
已采纳

条件逻辑形式验证

I'm having some issues writing logic for a script. I can't seem to wrap my head around it.

I have a form that contains 6 quantity fields, and two corresponding check boxes for each quantity field.

Quantity fields are referenced in variables $q1, $q2, $q3, $q4, $q5, $q6. Check boxes are referenced in variables $c1_1, $c1_2, $c2_1, $c2_2... etc.

The logic I want to achieve is, if you input a number into a quantity field, either one of the two corresponding checkboxes must be checked as well or the form invalidates.

My current code looks like this:

if(
   ($q1 !== "" && ($c1_1 == "" || $c1_2 == "")) ||
   ($q2 !== "" && ($c2_1 == "" || $c2_2 == "")) ||
   ($q3 !== "" && ($c3_1 == "" || $c3_2 == "")) ||
   ($q4 !== "" && ($c4_1 == "" || $c4_2 == "")) ||
   ($q5 !== "" && ($c5_1 == "" || $c5_2 == "")) ||
   ($q6 !== "" && ($c6_1 == "" || $c6_2 == ""))
) {
   $is_valid = false;
} else {
   $is_valid = true;
}

What's wrong with that code is that when I go to test that script, it requires both checkboxes to be checked to validate.

  • 写回答

2条回答 默认 最新

  • duanjiong2021 2013-07-24 05:32
    关注

    If I understood correctly, you want to invalidate if you meet one of this:

    • a field is empty
    • both checkboxes (belonging to a field) are unchecked

    so it would be

    if(
       ($q1 !== "" && ($c1_1 == "" && $c1_2 == "")) ||
       ($q2 !== "" && ($c2_1 == "" && $c2_2 == "")) ||
       ($q3 !== "" && ($c3_1 == "" && $c3_2 == "")) ||
       ($q4 !== "" && ($c4_1 == "" && $c4_2 == "")) ||
       ($q5 !== "" && ($c5_1 == "" && $c5_2 == "")) ||
       ($q6 !== "" && ($c6_1 == "" && $c6_2 == ""))
    ) {
       $is_valid = false;
    } else {
       $is_valid = true;
    }
    

    with or without the inner ().

    haven't tried myself, but should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)