使用Javascript / jQuery进行Bootstrap 4表单验证

时间:2018-02-14 21:43:46

标签: javascript jquery twitter-bootstrap validation bootstrap-4

我是网络开发的新手,我正在尝试设置练习表来学习表单验证。我正在关注Bootstrap的doc for Custom Styles来处理那些使用屏幕阅读器的可访问性问题,并为不同浏览器的用户标准化验证外观。

我已经设置了一个基本表单,并尝试使用jQuery创建一个JavaScript函数来处理验证。我觉得我几乎就在那里,但仍然没有像Bootstrap演示显示那样进行验证。

的index.html

<form id="signup-form" novalidate>

  <div class="form-group">
    <h1>Sign Up</h1>
    <p>Use this form to enter your name and email to sign up.</p>
  </div>

  <!-- Name -->
  <div class="form-group">
    <div class="row">
      <div class="col">
        <label for="firstNameInput">Name</label>
        <input type="text" class="form-control" id="firstNameInput" placeholder="First name" required>
        <div class="valid-feedback">
          Looks good!
        </div>
      </div>
      <div class="col">
        <label for="lastNameInput">Last</label>
        <input type="text" class="form-control" id="lastNameInput" placeholder="Last name" required>
        <div class="valid-feedback">
          Looks good!
        </div>
      </div>
    </div>
  </div>

  <!-- Email -->
  <div class="form-group">
    <label for="emailAddressInput">Email address</label>
    <input type="email" class="form-control" id="emailAddressInput" aria-describedby="emailHelp" placeholder="name@example.com" required>
    <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
    <div class="invalid-feedback">
      Please provide a valid email.
    </div>
  </div>

  <!-- Options -->
  <div class="form-group">
    <div class="form-check">
      <input class="form-check-input" type="radio" name="Radios" id="type1RadioButton" value="option1" checked>
      <label class="form-check-label" for="type1RadioButton">
        Type 1 user
      </label>
    </div>
    <div class="form-check">
      <input class="form-check-input" type="radio" name="Radios" id="type2RadioButton" value="option2">
      <label class="form-check-label" for="type2RadioButton">
        Type 2 user
      </label>
    </div>
  </div>

  <!-- Submit Button -->
  <div class="form-group">
    <a href="#" class="btn btn-primary" id="submitButton" type="submit">Submit</a>
  </div>

</form>

的script.js

$(document).ready(function() {
  $("#submitButton").click(function() {

    //Fetch form to apply custom Bootstrap validation
    var form = $("#signup-form")
    //alert(form.prop('id')) //test to ensure calling form correctly

    if (form.checkValidity() === false) {
      event.preventDefault()
      event.stopPropagation()
    }
    form.addClass('was-validated')
  })
})

1 个答案:

答案 0 :(得分:6)

问题是第一个表单节点上存在checkValidity(),而不是表单。

更改此内容:form.checkValidity() === false

form[0].checkValidity() === false

并且有效:https://www.codeply.com/go/pUPBLzn6yL

$(document).ready(function() {
  $("#submitButton").click(function() {

    //Fetch form to apply custom Bootstrap validation
    var form = $("#signup-form")
    alert(form.prop('id')) //test to ensure calling form correctly

    if (form[0].checkValidity() === false) {
      event.preventDefault()
      event.stopPropagation()
    }
    form.addClass('was-validated');

    //Make ajax call here

  })
})


有关Bootstrap 4验证的更多信息:
Can't make the validation work in Bootstrap 4