Form Validation
Add novalidate and the .was-validated class after a failed submit to show browser validation styles. Use .is-valid, .is-invalid, .valid-feedback and .invalid-feedback.
Example
<form class="needs-validation" novalidate>
<input class="form-control" required>
<div class="invalid-feedback">Please fill this in.</div>
<button class="btn btn-primary">Submit</button>
</form>
<script>
document.querySelector(".needs-validation").addEventListener("submit", e => {
if (!e.target.checkValidity()) e.preventDefault();
e.target.classList.add("was-validated");
});
</script>Exercise
Class added after submit.
was-Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.