diff options
| author | XhmikosR <[email protected]> | 2020-05-02 18:39:41 +0300 |
|---|---|---|
| committer | XhmikosR <[email protected]> | 2020-06-03 15:57:20 +0300 |
| commit | 6824d67beb423d51d27b9f297f97c75f6298c183 (patch) | |
| tree | bb26e31d1aa12339c53ededf74282cdd9ebca08f | |
| parent | 08742abda3a7e9717c5c6508537191e68986a24e (diff) | |
| download | bootstrap-6824d67beb423d51d27b9f297f97c75f6298c183.tar.xz bootstrap-6824d67beb423d51d27b9f297f97c75f6298c183.zip | |
Fix forms validation snippets.
| -rw-r--r-- | site/assets/js/validate-forms.js | 24 | ||||
| -rw-r--r-- | site/content/docs/5.0/forms/validation.md | 4 |
2 files changed, 26 insertions, 2 deletions
diff --git a/site/assets/js/validate-forms.js b/site/assets/js/validate-forms.js new file mode 100644 index 000000000..b79f63f2d --- /dev/null +++ b/site/assets/js/validate-forms.js @@ -0,0 +1,24 @@ +// 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') + + if (!forms) { + return + } + + // 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) + }) +})() diff --git a/site/content/docs/5.0/forms/validation.md b/site/content/docs/5.0/forms/validation.md index 2b8e1b7aa..c50516362 100644 --- a/site/content/docs/5.0/forms/validation.md +++ b/site/content/docs/5.0/forms/validation.md @@ -96,8 +96,9 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun <button class="btn btn-primary" type="submit">Submit form</button> </div> </form> +{{< /example >}} -<script> +{{< example lang="js" show_preview="false" >}} // Example starter JavaScript for disabling form submissions if there are invalid fields (function () { 'use strict'; @@ -118,7 +119,6 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun }, false); }); })(); -</script> {{< /example >}} ## Browser defaults |
