description: Provide valuable, actionable feedback to your users with HTML5 form validation, via browser default behaviors or custom styles and JavaScript.
group: forms
toc: true
+extra_js:
+ - src: "/docs/5.0/assets/js/validate-forms.js"
+ async: true
---
{{< callout warning >}}
{{< /example >}}
{{< example lang="js" show_preview="false" >}}
-// 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);
- });
-})();
+{{< js.inline >}}
+{{- readFile (printf "site/static/docs/%s/assets/js/validate-forms.js" .Site.Params.docs_version) -}}
+{{< /js.inline >}}
{{< /example >}}
## Browser defaults
// Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.querySelectorAll('.needs-validation')
- if (!forms) {
- return
- }
-
// Loop over them and prevent submission
Array.prototype.slice.call(forms)
.forEach(function (form) {