Skip to content

Commit

Permalink
Fix forms validation snippets.
Browse files Browse the repository at this point in the history
  • Loading branch information
XhmikosR authored and olsza committed Oct 3, 2020
1 parent a8db5d4 commit a9cac1a
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
24 changes: 24 additions & 0 deletions site/assets/js/validate-forms.js
Original file line number Diff line number Diff line change
@@ -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)
})
})()
4 changes: 2 additions & 2 deletions site/content/docs/5.0/forms/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -118,7 +119,6 @@ Custom feedback styles apply custom colors, borders, focus styles, and backgroun
}, false);
});
})();
</script>
{{< /example >}}

## Browser defaults
Expand Down

0 comments on commit a9cac1a

Please sign in to comment.