Bootstrap 5 Form Validation: Beyond the Submit Button
September 10, 2026
- Javascript
- Bootstrap
Bootstrap 5 Form Validation: Beyond the Submit Button
Bootstrap doesn't validate anything itself — it styles the results of the
browser's own Constraint Validation API (required, pattern, minlength,
checkValidity(), and friends). The docs show the standard recipe: block
submit, call checkValidity(), toggle a class. That covers "did the user
fill this in before hitting the button" but not two things that come up
immediately in practice — feedback before submit, and rules the HTML
attributes can't express, like "these two fields must match."
Option 1: The standard recipe — validate on submit
Add novalidate to the form (so the browser's default error bubbles don't
fight with Bootstrap's), give fields required/pattern/etc., and add an
.invalid-feedback div after each one. On submit, check validity and add
.was-validated — that class is what turns on Bootstrap's :invalid/:valid
styling.
<form class="needs-validation row g-2" novalidate>
<div class="col-12">
<label class="form-label" for="email">Email</label>
<input type="email" class="form-control" id="email" required>
<div class="invalid-feedback">Enter a valid email address.</div>
</div>
<div class="col-12">
<button class="btn btn-primary" type="submit">Submit</button>
</div>
</form>Note that none of the red/green styling is manual — .is-invalid/.is-valid
classes are never touched. Once .was-validated is on the <form>, Bootstrap's
CSS keys off the fields' own :invalid/:valid pseudo-classes automatically.
Option 2: Validating as the user types
The submit-only recipe means a user filling in a form field-by-field gets no
feedback until they've finished everything and clicked submit. For fields
worth correcting early — a username pattern, a taken email — validate on
input instead, toggling the classes yourself since there's no .was-validated
form-level switch to flip per field.
const username = document.getElementById('username');
function validateField(field) {
field.classList.toggle('is-valid', field.checkValidity());
field.classList.toggle('is-invalid', !field.checkValidity());
}
username.addEventListener('input', () => validateField(username));This is worth doing selectively, not for every field on every form — validating on every keystroke before someone's finished typing an email address just flashes red at them for no reason. Reserve it for fields with an instantly checkable shape, like a username pattern.
Option 3: Rules HTML attributes can't express
required, pattern, min/max, and minlength cover a single field in
isolation. "Confirm password must match password" depends on a second field,
and there's no attribute for that — setCustomValidity() is the escape
hatch. Passing it a non-empty string marks the field invalid with that
message as the reason; passing '' clears it.
const pw = document.getElementById('pw');
const pw2 = document.getElementById('pw2');
function checkMatch() {
// The pattern/required attributes can't express "matches another
// field", so setCustomValidity fills that gap by hand.
pw2.setCustomValidity(pw2.value !== pw.value ? 'mismatch' : '');
}
pw.addEventListener('input', checkMatch);
pw2.addEventListener('input', checkMatch);checkValidity() and .was-validated styling both respect a custom
validity message exactly like a failed pattern — the field just shows
invalid until setCustomValidity('') clears it.
Which one should you use?
| Situation | Use |
|---|---|
| Simple required/format checks, low field count | Submit-time was-validated |
| A field worth correcting before submit | input-time class toggling |
| A rule spanning more than one field | setCustomValidity() |
They combine freely — the password example above still uses was-validated
on submit for the rest of the form, with setCustomValidity layered in only
for the one cross-field rule.
Two gotchas
novalidate on the <form> is not optional. Without it, the browser's
own validation bubbles fire and block submit before your handler even runs,
so Bootstrap's styling never gets a chance to apply. It doesn't disable
validation — checkValidity() still works — it only turns off the browser's
default UI for it.
Custom validity messages don't clear themselves. If setCustomValidity
set a field invalid, no later required/pattern re-check clears it —
only calling setCustomValidity('') does. Forget that, and a field that's
since become valid stays stuck red because a stale custom message is still
attached to it.
Bootstrap's .was-validated recipe is really just the on-submit case of a
more general tool — checkValidity() plus setCustomValidity() will answer
almost any validation question you throw at a form, submit-time or not.