繁体   English   中英

复选框和单选按钮的Javascript验证

[英]Javascript Validation of both Checkboxes and Radio Buttons

我正在做一份清单,每次工作人员每次填写此特定表格时都需要填写。 请注意,由于没有足够的时间进行研究,我仍在慢慢地掌握JS。

我做了一些研究,并结合了一些不同的验证样式,并提出了解决方案。

function validate(form) {
    var e = form.elements;

    if(e['reloaded'].value == "yes") {
        if(!e['Q1-A'].value) {
            alert('You Must complete the Checklist!');
            return false;
        }
        if(!e['Q1-B'].value) {
            alert('You Must complete the Checklist!');
            return false;
        }
        if(!e['Q1-C'].value) {
            alert('You Must complete the Checklist!');
            return false;
        }
        if(!e['Q1-D'].value) {
            alert('You Must complete the Checklist!');
            return false;
        }
        if(!e['Q1-E'].value) {
            alert('You Must complete the Checklist!');
            return false;
        }
        if(!e['Q1-G'].value) {
            alert('You Must complete the Checklist!');
            return false;
        }
        if(!e['Gen-A'].value) {
            alert('You Must complete the Checklist!');
            return false;
        }
        if(!e['Gen-B'].value) {
            alert('You Must complete the Checklist!');
            return false;
        }
        if(!e['invoice_number'].value) {
            alert('Invoice Number is REQUIRED!');
            return false;
        }
        return true;

    } else if(e['reloaded'].value == "no") {
        if(!e['Gen-A'].value) {
            alert('You Must complete the Checklist!');
            return false;
        }
        if(!e['Gen-B'].value) {
            alert('You Must complete the Checklist!');
            return false;
        }
        if(!e['Q2-A'].value) {
            alert('You Must complete the Checklist!');
            return false;
        }
        if(!e['invoice_number'].value) {
            alert('Invoice Number is REQUIRED!');
            return false;
        }
        return true;

    } else if(!e['reloaded'].value) {
        alert('You must indicate if you Reloaded the OS!');
        return false;
    }
}

在此阶段,它只是返回“您必须指出是否重新加载了操作系统!”。 即使选择它。

关键是从单选按钮中选择一个选项,然后它将显示一些复选框,并且必须先选中所有复选框,然后才能提交表单。 显然,某些复选框仅显示是否选择了特定选项,因此需要将某些选项排除在外。 还有一个需要填写的附加字段(invoice_number),始终对其进行验证并且一直有效。

单选按钮:

<input name="reloaded" id="reloaded" type="radio" value="yes" onClick="Q1(this.value);" /> Yes</label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <label><input name="reloaded" id="reloaded" type="radio" value="no" onClick="Q1(this.value);" />

除ID /名称外,所有复选框均相同

<input name="Q1-A" id="Q1-A" type="checkbox" value="yes" />

必填文本框:

<input class="field size5" type="text" name="invoice_number" id="invoice_number" placeholder="Required! - 'RA' for Warranty Jobs" />

我确信由于我缺乏知识,我已经使它复杂化了,但是我只是想不出哪里出了问题。

(通过onsubmit="return validate(this);"提交)

以下代码是我的最终结果,它可以工作;

                function validate(form) {
                var e = form.elements;

              if(document.getElementById('RL-Y').checked) {
                  if(!e['Q1-A'].checked || !e['Q1-B'].checked || !e['Q1-C'].checked || !e['Q1-D'].checked || !e['Q1-E'].checked || !e['Q1-G'].checked || !e['Gen-A'].checked || !e['Gen-B'].checked) {
                    alert('You Must complete the Checklist!');
                    return false;
                  }
                  if(!e['invoice_number'].value) {
                    alert('Invoice Number is REQUIRED!');
                    return false;
                  }
                  return true;

              } else if(document.getElementById('RL-N').checked) {
                  if(!e['Gen-A'].checked || !e['Gen-B'].checked || !e['Q2-A'].checked) {
                    alert('You Must complete the Checklist!');
                    return false;
                  }
                  if(!e['invoice_number'].value) {
                    alert('Invoice Number is REQUIRED!');
                    return false;
                  }
                  return true;

              } else {
                alert('You must indicate if you Reloaded the OS!');
                return false;
              }
            }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM