Form validation is vital to protect applications from bad user inputs, prevent database errors, and provide immediate, user-friendly guidance. Bootstrap 5 leverages native HTML5 constraint validation attributes (required, pattern, minlength) while replacing ugly default browser tooltips with custom green/red feedback icons and messages.
Bootstrap's validation cycle works in three coordinated steps:
1. Novalidate Attribute: Add novalidate to your <form> to disable default browser bubble tooltips.
2. JavaScript Interception: Intercept the form's submit event using event.preventDefault() if form.checkValidity() returns false.
3. Apply .was-validated Class: Adding .was-validated to the form instructs Bootstrap's CSS to evaluate :valid and :invalid pseudo-classes across all inputs instantly.
Here is a production-ready registration form with client-side validation and feedback messages:
<form class="row g-3 needs-validation" novalidate>
<!-- First Name -->
<div class="col-md-6">
<label for="validationFirstName" class="form-label">First Name</label>
<input type="text" class="form-control" id="validationFirstName" required>
<div class="valid-feedback">Looks great!</div>
<div class="invalid-feedback">Please provide your first name.</div>
</div>
<!-- Email Address -->
<div class="col-md-6">
<label for="validationEmail" class="form-label">College Email</label>
<input type="email" class="form-control" id="validationEmail" required>
<div class="invalid-feedback">Please enter a valid email address.</div>
</div>
<!-- Checkbox Agreement -->
<div class="col-12">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
<label class="form-check-label" for="invalidCheck">
Agree to college lab terms and conditions
</label>
<div class="invalid-feedback">You must agree before submitting.</div>
</div>
</div>
<!-- Submit Button -->
<div class="col-12">
<button class="btn btn-primary" type="submit">Submit Form</button>
</div>
</form>
<!-- Bootstrap 5 Validation JavaScript Starter -->
<script>
(() => {
'use strict';
// Fetch all forms with needs-validation class
const forms = document.querySelectorAll('.needs-validation');
Array.from(forms).forEach(form => {
form.addEventListener('submit', event => {
if (!form.checkValidity()) {
event.preventDefault();
event.stopPropagation();
}
form.classList.add('was-validated');
}, false);
});
})();
</script>
When performing asynchronous server validation (e.g. checking if a username is already taken in MySQL or MongoDB), you can manually apply .is-valid or .is-invalid classes directly to input elements via JavaScript or backend templates (PHP / Node.js) without needing the .was-validated form wrapper.
Strengthen your responsive web design mastery by studying these connected classes:
Understand base input elements, required attributes, and form structure.
Combine floating label animations with .is-valid and .is-invalid.
Display error messages as floating bubbles with .invalid-tooltip.
Prevent default form submission in JavaScript until inputs pass validation.
novalidate attribute instructs the browser not to display default native browser validation bubbles, allowing Bootstrap's custom CSS classes and feedback messages to take over.
.invalid-feedback with .invalid-tooltip. Ensure the parent container has position: relative; so the tooltip positions correctly.
pattern attribute on the password input (e.g. pattern='(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}'), and display detailed requirements inside the .invalid-feedback div.
Advance your practical frontend engineering skills with the next topic in our synchronized curriculum.
Continue to Class 18: Responsive Navbars → Previous: ← Class 16: Floating Labels | Full Syllabus