1
0
mirror of https://github.com/twbs/bootstrap.git synced 2025-08-18 11:21:23 +02:00

Read validate-forms.js from assets.

This commit is contained in:
XhmikosR
2020-05-02 18:49:12 +03:00
parent 6824d67beb
commit 358d637d1c
3 changed files with 10 additions and 24 deletions

View File

@@ -4,6 +4,9 @@ title: Validation
description: Provide valuable, actionable feedback to your users with HTML5 form validation, via browser default behaviors or custom styles and JavaScript. description: Provide valuable, actionable feedback to your users with HTML5 form validation, via browser default behaviors or custom styles and JavaScript.
group: forms group: forms
toc: true toc: true
extra_js:
- src: "/docs/5.0/assets/js/validate-forms.js"
async: true
--- ---
{{< callout warning >}} {{< callout warning >}}
@@ -99,26 +102,9 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun
{{< /example >}} {{< /example >}}
{{< example lang="js" show_preview="false" >}} {{< example lang="js" show_preview="false" >}}
// Example starter JavaScript for disabling form submissions if there are invalid fields {{< js.inline >}}
(function () { {{- readFile (printf "site/static/docs/%s/assets/js/validate-forms.js" .Site.Params.docs_version) -}}
'use strict'; {{< /js.inline >}}
// 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);
});
})();
{{< /example >}} {{< /example >}}
## Browser defaults ## Browser defaults

View File

@@ -47,5 +47,9 @@
{{ partial "footer" . }} {{ partial "footer" . }}
{{ partial "scripts" . }} {{ partial "scripts" . }}
{{ range .Page.Params.extra_js -}}
<script{{ with .async }} async{{ end }} src="{{ .src }}"></script>
{{- end -}}
</body> </body>
</html> </html>

View File

@@ -5,10 +5,6 @@
// Fetch all the forms we want to apply custom Bootstrap validation styles to // Fetch all the forms we want to apply custom Bootstrap validation styles to
var forms = document.querySelectorAll('.needs-validation') var forms = document.querySelectorAll('.needs-validation')
if (!forms) {
return
}
// Loop over them and prevent submission // Loop over them and prevent submission
Array.prototype.slice.call(forms) Array.prototype.slice.call(forms)
.forEach(function (form) { .forEach(function (form) {