After exploring the form validation section in Bootstrap 4.4 (https://getbootstrap.com/docs/4.4/components/forms/#how-it-works), I have successfully integrated the code into my project. The code example and script for triggering validation seem quite impressive!
I've been pondering whether it's feasible to enable this validation while the user is still filling out the form, rather than waiting until they attempt to submit it?
For example, if there are two required input fields - First Name & Last Name, can I validate each field as soon as it's filled out (e.g., changing focus from one field to another)?
In other words, real-time validation?
The current validation method I'm using (activated when clicking the form submit button) is:
function validateForm () {
var forms = document.getElementsByClassName('needs-validation')
var validation = Array.prototype.filter.call(forms, function(form) {
console.log(form.checkValidity())
if (form.checkValidity() === false) {
event.preventDefault()
event.stopPropagation()
// get the "first" invalid field
var errorElements = document.querySelectorAll('.form-control:invalid')
// scroll the user to the invalid field
window.scrollTo(0, getOffset(errorElements[0]).top)
}
form.classList.add('was-validated')
})
}