bootstrapV4表单验证

Provide valuable, actionable feedback to your users with HTML5 form validation–available in all our supported browsers. Choose from the browser default validation feedback, or implement custom messages with our built-in classes and starter JavaScript.

通过HTML5表单验证为用户提供有价值的、可操作的反馈信息-可在所有支持的浏览器中使用。可以选择从浏览器的默认验证反馈,或者使用我们内置类和starter javascript实现自定义消息。

 

How it works
工作原理

Here’s how form validation works with Bootstrap:
这是表单验证功能在bootstrap中的工作原理:

  • HTML form validation is applied via CSS’s two pseudo-classes, :invalid and :valid. It applies to <input>, <select>, and <textarea> elements.
    在HTML中表单验证功能是通过CSS的两个伪类::invalid和:valid来应用的,它可以应用于<input>、<select>和<textarea>元素。
  • Bootstrap scopes the :invalid and :valid styles to parent .was-validated class, usually applied to the <form>.
    Bootstrap将:invalid和:valid伪类应用于父级元素,将.was-validated样式通常应用于<form>元素。
  • Otherwise, any required field without a value shows up as invalid on page load. This way, you may choose when to activate them (typically after form submission is attempted).
    另外,任何没有输入值的必填项显示为无效。这样以来,你就可以选择什么时候激活他们(通常在用户尝试表单的提交行为时)。
  • To reset the appearance of the form (for instance, in the case of dynamic form submissions using AJAX),
    重新渲染表单显示的步骤(例如,在使用AJAX动态提交过表单时)
  • remove the .was-validated class from the <form> again after submission.
    再次提交表单后从<form>元素上删除.was-validated类
  • As a fallback, .is-invalid and .is-valid classes may be used instead of the pseudo-classes for server side validation.
    如果用在服务器端,则需要使用.is-invalid和.is-valid类来代替两个伪类
  • They do not require a .was-validated parent class.
    并且不需要在父级元素使用.was-validated类
  • Due to constraints in how CSS works,
    由于CSS工作方式的限制
  • we cannot (at present) apply styles to a <label> that comes before a form control in the DOM without the help of custom JavaScript.
    如果没有自定义Javascript的帮助,我们(目前)不能应用样式到表单控件前面的<label>。
  • All modern browsers support the constraint validation API, a series of JavaScript methods for validating form controls.
    所有现代浏览器都支持约束验证API,一系列用于表单控件的JavaScript方法
  • Feedback messages may utilize the browser defaults (different for each browser, and unstylable via CSS) or our custom feedback styles with additional HTML and CSS.
    反馈消息可以使用浏览器默认设置(不同浏览器的会有区别,且没法使用CSS来修饰)或者使用我们基于传统HTML和CSS自定义的反馈信息样式。
  • You may provide custom validity messages with setCustomValidity in JavaScript.
    你可以在JavaScript中使用setCustomValidity提供自定义有效信息


With that in mind, consider the following demos for our custom form validation styles, optional server side classes, and browser defaults.
考虑到这一点,请考虑以下关于:自定义表单验证样式、可选服务器端类和浏览器默认样式的演示。

 

Custom styles
自定义样式

For custom Bootstrap form validation messages, you’ll need to add the novalidate boolean attribute to your <form>.
用于自定义bootstrap表单验证信息、你可能需要增加novalidate布尔属性到form标签中。

This disables the browser default feedback tooltips, but still provides access to the form validation APIs in JavaScript.
这将禁用浏览器默认反馈提示、但仍然提供通往javascript中的表单验证API的访问。
Try to submit the form below; our JavaScript will intercept the submit button and relay feedback to you. When attempting to submit, you’ll see the :invalid and :valid styles applied to your form controls.
请尝试提交下面的表单;我们的javascript将截听提交按钮并且把反馈转达给你。当我们尝试提交时、你将看到:invalid和:valid样式被应用到你的表单控件上。

Custom feedback styles apply custom colors, borders, focus styles, and background icons to better communicate feedback. Background icons for <select>s are only available with .custom-select, and not .form-control.
自定义反馈信息样式使用自定义颜色、边框、获取焦点时的样式和背景图标来更好的传达反馈信息。用于select控件的背景图标只适用于.custom-select,不适用于.form-control。

 

Customizing
自定义

Validation states can be customized via Sass with the $form-validation-states map.
验证状态可以在Sass中使用$form-validation-states map来自己定义。
Located in our _variables.scss file, this Sass map is looped over to generate the default valid/invalid validation states. Included is a nested map for customizing each state’s color and icon.
在_variables.scss文件中,这个Sass map被循环应用来生成默认的“有效/无效”验证状态。包括一个嵌套的map来自定义每个状态的颜色和图标。
While no other states are supported by browsers, those using custom styles can easily add more complex form feedback.
虽然浏览器不支持其他状态,但使用自定义样式的浏览器可以轻松添加更复杂的表单反馈。

Please note that we do not recommend customizing these values without also modifying the form-validation-state mixin.
请注意,我们不建议在不修改form-validation-state mixin的情况下自定义这些值。

posted on 2022-01-19 09:51  前端码牛  阅读(900)  评论(0编辑  收藏  举报

导航