【问题标题】:validate two checkboxes of a Bootstrap 4 form验证 Bootstrap 4 表单的两个复选框
【发布时间】:2020-03-16 05:53:59
【问题描述】:

在 Bootstrap 4 中,我需要使用两个复选框来验证表单:

<form class="needs-validation" novalidate>
  <div class="form-group row">
    <label for="validationCustom01">First name</label>
    <input type="text" class="form-control" id="validationCustom01" placeholder="First name" required>
  </div>
  <div class="form-group row">
    <label for="validationCustom02">Last name</label>
    <input type="text" class="form-control" id="validationCustom02" placeholder="Last name" required>
  </div>
  <div class="form-check form-check-inline">
    <input class="form-check-input" type="checkbox" id="inlineCheckbox1">
    <label class="form-check-label" for="inlineCheckbox1">1</label>
  </div>
  <div class="form-check form-check-inline">
    <input class="form-check-input" type="checkbox" id="inlineCheckbox2">
    <label class="form-check-label" for="inlineCheckbox2">2</label>
  </div>

  <button class="btn btn-primary" type="submit">Submit form</button>
</form>

<script>
  // Example starter JavaScript for disabling form submissions if there are invalid fields
  (function() {
    'use strict';
    window.addEventListener('load', function() {
      // Fetch all the forms we want to apply custom Bootstrap validation styles to
      var forms = document.getElementsByClassName('needs-validation');
      // Loop over them and prevent submission
      var validation = Array.prototype.filter.call(forms, function(form) {
        form.addEventListener('submit', function(event) {
          if (form.checkValidity() === false) {
            event.preventDefault();
            event.stopPropagation();
          }
          form.classList.add('was-validated');
        }, false);
      });
    }, false);
  })();
</script>

如果至少选中一个复选框,则该表单有效。

我是 Bootstrap 和 Javascript 的新手。如何将复选框的验证添加到上面的脚本中?

【问题讨论】:

    标签: javascript twitter-bootstrap validation bootstrap-4


    【解决方案1】:

    我会做这样的事情。获取复选框的引用并添加额外的验证。

    <form class="needs-validation" novalidate>
      <div class="form-group row">
        <label for="validationCustom01">First name</label>
        <input type="text" class="form-control" id="validationCustom01" placeholder="First name" required>
      </div>
      <div class="form-group row">
        <label for="validationCustom02">Last name</label>
        <input type="text" class="form-control" id="validationCustom02" placeholder="Last name" required>
      </div>
      <div class="form-check form-check-inline">
        <input class="form-check-input" type="checkbox" id="inlineCheckbox1">
        <label class="form-check-label" for="inlineCheckbox1">1</label>
      </div>
      <div class="form-check form-check-inline">
        <input class="form-check-input" type="checkbox" id="inlineCheckbox2">
        <label class="form-check-label" for="inlineCheckbox2">2</label>
      </div>
    
      <button class="btn btn-primary" type="submit">Submit form</button>
    </form>
    
    <script>
      // Example starter JavaScript for disabling form submissions if there are invalid fields
      (function() {
        'use strict';
        window.addEventListener('load', function() {
          // Fetch all the forms we want to apply custom Bootstrap validation styles to
          var forms = document.getElementsByClassName('needs-validation');
          // Loop over them and prevent submission
          var validation = Array.prototype.filter.call(forms, function(form) {
            form.addEventListener('submit', function(event) {
            // Get the first checkbox
            const checkbox1 = form.querySelector('#inlineCheckbox1');
            // Get the second checkbox
            const checkbox2 = form.querySelector('#inlineCheckbox2');
            // Detect if atleast one checkbox is checked
            const isCheckboxChecked = checkbox1.checked || checkbox2.checked;
            
              // If form is invalid or at least one checkbox is not checked -> fail validation
              if (form.checkValidity() === false || isCheckboxChecked === false) {
                event.preventDefault();
                event.stopPropagation();
              }
              form.classList.add('was-validated');
            }, false);
          });
        }, false);
      })();
    </script>

    【讨论】:

    • 谢谢。如果没有选择至少一个复选框,我如何向用户提供反馈? IE。将复选框标记为红色并显示文本“请至少选择一个复选框”。
    • 您可以使用您的消息创建和附加元素,或者在该消息上切换类,例如在display: nonedisplay: block 之间切换。例如,if (!isCheckboxChecked) { document.querySelector('&lt;path_to_your_element&gt;').classlist.toggle('error'); } else { document.querySelector('&lt;path_to_your_element&gt;').classlist.toggle('error'); }
    猜你喜欢
    • 1970-01-01
    • 2014-12-04
    • 1970-01-01
    • 2017-05-04
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    相关资源
    最近更新 更多