表单验证

通过浏览器默认行为或自定义样式和JavaScript,通过HTML5表单验证为用户提供有价值的、可操作的反馈。

更新于: 2023-04-30 14:28:21 查看: 570

发布于: 2021-9-1 00:07 发布者: 大米

工作原理 

下面是表单验证如何使用Bootstrap:

  • HTML表单验证通过CSS的两个伪类: :invalid:valid 应用。它适用于 <input><select><textarea> 元素。
  • 要重置表单的外观(例如,在使用AJAX动态提交表单的情况下),请在提交之后再次从 <form> 中删除 .was-validated 类。
  • 作为回退, .is-invalid.is-valid 可以代替伪类用于 服务器端验证。它们不需要 .was-validated 的父类。
  • 由于CSS工作方式的限制,在没有定制JavaScript的帮助下,我们无法(目前)将样式应用于DOM中表单控件之前的 <label>
  • 所有现代浏览器都支持 约束验证API,这是一系列用于验证表单控件的JavaScript方法。
  • 反馈信息可以使用 浏览器默认设置(每种浏览器不同,通过CSS不可升级)或我们的自定义反馈样式以及附加的HTML和CSS。
  • 您可以在JavaScript中使用 setCustomValidity 提供自定义有效性消息

考虑到这一点,请考虑以下自定义表单验证样式、可选的服务器端类和浏览器默认设置的演示。

自定义样式

对于自定义Bootstrap表单验证信息,需要将 novalidate 布尔属性添加到 <form>。但默认情况下,JavaScript仍会在表单验证中禁用提示工具。尝试提交下面的表单;我们的JavaScript将拦截submit按钮并将反馈信息转发给您。尝试提交时,您将看到 :invalid 和 :valid 样式应用于表单控件。

自定义反馈样式应用自定义颜色、边框、焦点样式和背景图标,以便更好地传达反馈。 <select> 的背景图标仅适用于 .form-select,而不是 .form-control控件。
Looks good!
Looks good!
@
Please choose a username.
Please provide a valid city.
Please select a valid state.
Please provide a valid zip.
You must agree before submitting.
<form class="row g-3 needs-validation" novalidate>
<div class="col-md-4">
<label for="validationCustom01" class="form-label">First name</label>
<input type="text" class="form-control" id="validationCustom01" value="Mark" required>
<div class="valid-feedback">
  Looks good!
</div>
</div>
<div class="col-md-4">
<label for="validationCustom02" class="form-label">Last name</label>
<input type="text" class="form-control" id="validationCustom02" value="Otto" required>
<div class="valid-feedback">
  Looks good!
</div>
</div>
<div class="col-md-4">
<label for="validationCustomUsername" class="form-label">Username</label>
<div class="input-group has-validation">
  <span class="input-group-text" id="inputGroupPrepend">@</span>
  <input type="text" class="form-control" id="validationCustomUsername" aria-describedby="inputGroupPrepend" required>
  <div class="invalid-feedback">
    Please choose a username.
  </div>
</div>
</div>
<div class="col-md-6">
<label for="validationCustom03" class="form-label">City</label>
<input type="text" class="form-control" id="validationCustom03" required>
<div class="invalid-feedback">
  Please provide a valid city.
</div>
</div>
<div class="col-md-3">
<label for="validationCustom04" class="form-label">State</label>
<select class="form-select" id="validationCustom04" required>
  <option selected disabled value="">Choose...</option>
  <option>...</option>
</select>
<div class="invalid-feedback">
  Please select a valid state.
</div>
</div>
<div class="col-md-3">
<label for="validationCustom05" class="form-label">Zip</label>
<input type="text" class="form-control" id="validationCustom05" required>
<div class="invalid-feedback">
  Please provide a valid zip.
</div>
</div>
<div class="col-12">
<div class="form-check">
  <input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
  <label class="form-check-label" for="invalidCheck">
    Agree to terms and conditions
  </label>
  <div class="invalid-feedback">
    You must agree before submitting.
  </div>
</div>
</div>
<div class="col-12">
<button class="btn btn-primary" type="submit">Submit form</button>
</div>
</form>
// Example starter JavaScript for disabling form submissions if there are invalid fields
(function () {
'use strict'

// Fetch all the forms we want to apply custom Bootstrap validation styles to
  var forms = document.querySelectorAll('.needs-validation')

// Loop over them and prevent submission
  Array.prototype.slice.call(forms)
.forEach(function (form) {
  form.addEventListener('submit', function (event) {
    if (!form.checkValidity()) {
      event.preventDefault()
      event.stopPropagation()
    }

    form.classList.add('was-validated')
  }, false)
})
})()

浏览器默认设置

对自定义验证反馈消息或编写JavaScript来更改表单行为不感兴趣?很好,你可以使用浏览器的默认设置。请尝试提交下面的表格。根据您的浏览器和操作系统的不同,您将看到稍微不同的反馈样式。

虽然这些反馈样式不能用CSS设置样式,但是您仍然可以通过JavaScript自定义反馈文本。

@
<form class="row g-3">
<div class="col-md-4">
<label for="validationDefault01" class="form-label">First name</label>
<input type="text" class="form-control" id="validationDefault01" value="Mark" required>
</div>
<div class="col-md-4">
<label for="validationDefault02" class="form-label">Last name</label>
<input type="text" class="form-control" id="validationDefault02" value="Otto" required>
</div>
<div class="col-md-4">
<label for="validationDefaultUsername" class="form-label">Username</label>
<div class="input-group">
  <span class="input-group-text" id="inputGroupPrepend2">@</span>
  <input type="text" class="form-control" id="validationDefaultUsername"  aria-describedby="inputGroupPrepend2" required>
</div>
</div>
<div class="col-md-6">
<label for="validationDefault03" class="form-label">City</label>
<input type="text" class="form-control" id="validationDefault03" required>
</div>
<div class="col-md-3">
<label for="validationDefault04" class="form-label">State</label>
<select class="form-select" id="validationDefault04" required>
  <option selected disabled value="">Choose...</option>
  <option>...</option>
</select>
</div>
<div class="col-md-3">
<label for="validationDefault05" class="form-label">Zip</label>
<input type="text" class="form-control" id="validationDefault05" required>
</div>
<div class="col-12">
<div class="form-check">
  <input class="form-check-input" type="checkbox" value="" id="invalidCheck2" required>
  <label class="form-check-label" for="invalidCheck2">
    Agree to terms and conditions
  </label>
</div>
</div>
<div class="col-12">
<button class="btn btn-primary" type="submit">Submit form</button>
</div>
</form>

服务器端验证

我们建议使用客户端验证,但如果需要服务器端验证,则可以使用 .is-invalid .is-valid 指示无效和有效的表单字段。注意,这些类也支持 .invalid-feedback

对于无效字段,请确保使用 aria-describedby 将无效反馈/错误消息与相关表单字段相关联(注意,如果该字段已指向其他表单文本,则此属性允许引用多个 id )。

若要解决边框半径的问题,输入组需要额外的 .has-validation 类。

Looks good!
Looks good!
@
Please choose a username.
Please provide a valid city.
Please select a valid state.
Please provide a valid zip.
You must agree before submitting.
<form class="row g-3">
<div class="col-md-4">
<label for="validationServer01" class="form-label">First name</label>
<input type="text" class="form-control is-valid" id="validationServer01" value="Mark" required>
<div class="valid-feedback">
  Looks good!
</div>
</div>
<div class="col-md-4">
<label for="validationServer02" class="form-label">Last name</label>
<input type="text" class="form-control is-valid" id="validationServer02" value="Otto" required>
<div class="valid-feedback">
  Looks good!
</div>
</div>
<div class="col-md-4">
<label for="validationServerUsername" class="form-label">Username</label>
<div class="input-group has-validation">
  <span class="input-group-text" id="inputGroupPrepend3">@</span>
  <input type="text" class="form-control is-invalid" id="validationServerUsername" aria-describedby="inputGroupPrepend3 validationServerUsernameFeedback" required>
  <div id="validationServerUsernameFeedback" class="invalid-feedback">
    Please choose a username.
  </div>
</div>
</div>
<div class="col-md-6">
<label for="validationServer03" class="form-label">City</label>
<input type="text" class="form-control is-invalid" id="validationServer03" aria-describedby="validationServer03Feedback" required>
<div id="validationServer03Feedback" class="invalid-feedback">
  Please provide a valid city.
</div>
</div>
<div class="col-md-3">
<label for="validationServer04" class="form-label">State</label>
<select class="form-select is-invalid" id="validationServer04" aria-describedby="validationServer04Feedback" required>
  <option selected disabled value="">Choose...</option>
  <option>...</option>
</select>
<div id="validationServer04Feedback" class="invalid-feedback">
  Please select a valid state.
</div>
</div>
<div class="col-md-3">
<label for="validationServer05" class="form-label">Zip</label>
<input type="text" class="form-control is-invalid" id="validationServer05" aria-describedby="validationServer05Feedback" required>
<div id="validationServer05Feedback" class="invalid-feedback">
  Please provide a valid zip.
</div>
</div>
<div class="col-12">
<div class="form-check">
  <input class="form-check-input is-invalid" type="checkbox" value="" id="invalidCheck3" aria-describedby="invalidCheck3Feedback" required>
  <label class="form-check-label" for="invalidCheck3">
    Agree to terms and conditions
  </label>
  <div id="invalidCheck3Feedback" class="invalid-feedback">
    You must agree before submitting.
  </div>
</div>
</div>
<div class="col-12">
<button class="btn btn-primary" type="submit">Submit form</button>
</div>
</form>

表单支持元素

验证样式可用于以下表单控件和组件:

  • <input><textarea> 带有 .form-control (在输入组中最多包含一个 .form-control )
  • <select> 带有 .form-select
  • .form-check
Please enter a message in the textarea.
Example invalid feedback text
More example invalid feedback text
Example invalid select feedback
Example invalid form file feedback
<form class="was-validated">
<div class="mb-3">
<label for="validationTextarea" class="form-label">Textarea</label>
<textarea class="form-control is-invalid" id="validationTextarea" placeholder="Required example textarea" required></textarea>
<div class="invalid-feedback">
  Please enter a message in the textarea.
</div>
</div>

<div class="form-check mb-3">
<input type="checkbox" class="form-check-input" id="validationFormCheck1" required>
<label class="form-check-label" for="validationFormCheck1">Check this checkbox</label>
<div class="invalid-feedback">Example invalid feedback text</div>
</div>

<div class="form-check">
<input type="radio" class="form-check-input" id="validationFormCheck2" name="radio-stacked" required>
<label class="form-check-label" for="validationFormCheck2">Toggle this radio</label>
</div>
<div class="form-check mb-3">
<input type="radio" class="form-check-input" id="validationFormCheck3" name="radio-stacked" required>
<label class="form-check-label" for="validationFormCheck3">Or toggle this other radio</label>
<div class="invalid-feedback">More example invalid feedback text</div>
</div>

<div class="mb-3">
<select class="form-select" required aria-label="select example">
  <option value="">Open this select menu</option>
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>
<div class="invalid-feedback">Example invalid select feedback</div>
</div>

<div class="mb-3">
<input type="file" class="form-control" aria-label="file example" required>
<div class="invalid-feedback">Example invalid form file feedback</div>
</div>

<div class="mb-3">
<button class="btn btn-primary" type="submit" disabled>Submit form</button>
</div>
</form>

提示工具 

如果表单布局允许,可以将 .{valid|invalid}-feedback 类替换为 .{valid|invalid}-tooltip 类,以在样式化的提示工具中显示验证反馈。确保在提示工具定位时有一个带有 position: relative 的父级。在下面的示例中,我们的列类已经有了这种设置,但是您的项目可能需要另一种设置
Looks good!
Looks good!
@
Please choose a unique and valid username.
Please provide a valid city.
Please select a valid state.
Please provide a valid zip.
<form class="row g-3 needs-validation" novalidate>
<div class="col-md-4 position-relative">
<label for="validationTooltip01" class="form-label">First name</label>
<input type="text" class="form-control" id="validationTooltip01" value="Mark" required>
<div class="valid-tooltip">
  Looks good!
</div>
</div>
<div class="col-md-4 position-relative">
<label for="validationTooltip02" class="form-label">Last name</label>
<input type="text" class="form-control" id="validationTooltip02" value="Otto" required>
<div class="valid-tooltip">
  Looks good!
</div>
</div>
<div class="col-md-4 position-relative">
<label for="validationTooltipUsername" class="form-label">Username</label>
<div class="input-group has-validation">
  <span class="input-group-text" id="validationTooltipUsernamePrepend">@</span>
  <input type="text" class="form-control" id="validationTooltipUsername" aria-describedby="validationTooltipUsernamePrepend" required>
  <div class="invalid-tooltip">
    Please choose a unique and valid username.
  </div>
</div>
</div>
<div class="col-md-6 position-relative">
<label for="validationTooltip03" class="form-label">City</label>
<input type="text" class="form-control" id="validationTooltip03" required>
<div class="invalid-tooltip">
  Please provide a valid city.
</div>
</div>
<div class="col-md-3 position-relative">
<label for="validationTooltip04" class="form-label">State</label>
<select class="form-select" id="validationTooltip04" required>
  <option selected disabled value="">Choose...</option>
  <option>...</option>
</select>
<div class="invalid-tooltip">
  Please select a valid state.
</div>
</div>
<div class="col-md-3 position-relative">
<label for="validationTooltip05" class="form-label">Zip</label>
<input type="text" class="form-control" id="validationTooltip05" required>
<div class="invalid-tooltip">
  Please provide a valid zip.
</div>
</div>
<div class="col-12">
<button class="btn btn-primary" type="submit">Submit form</button>
</div>
</form>

扫描二维码,手机查看
声明:部分数据/图片来源互联网,不代表Hluse Doc.,真实性请妥善甄别。