Bootstrap 5 Form Validation
Good forms tell users what's wrong before they submit bad data. HTML already has built-in validation attributes (required, type="email", minlength, pattern, …). Bootstrap adds styles that show green and red borders, icons and feedback messages based on that validation.
Two ways to show validation styles
| Approach | How | When |
|---|---|---|
Browser validation + .was-validated |
Add novalidate to the form, then add .was-validated to it on submit. Bootstrap styles every field via :valid / :invalid |
Most forms |
| Manual classes | Add .is-valid or .is-invalid to individual fields yourself |
Server-side checks, custom rules |
Feedback messages go in .valid-feedback / .invalid-feedback elements placed right after the control; they're hidden until the field is styled as valid/invalid.
Browser validation with .was-validated
novalidate turns off the browser's own error bubbles so Bootstrap's messages can take over. On submit, a few lines of JavaScript check form.checkValidity(), cancel the submission if something's wrong and add .was-validated.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<form class="needs-validation row g-2" novalidate>
<div class="col-6">
<label for="user" class="form-label">Username</label>
<input class="form-control" id="user" required minlength="3">
<div class="valid-feedback">Looks good!</div>
<div class="invalid-feedback">At least 3 characters.</div>
</div>
<div class="col-6">
<label for="mail" class="form-label">Email</label>
<input type="email" class="form-control" id="mail" required>
<div class="invalid-feedback">Enter a valid email.</div>
</div>
<div class="col-12">
<div class="form-check">
<input class="form-check-input" type="checkbox" id="terms" required>
<label class="form-check-label" for="terms">I accept the terms</label>
<div class="invalid-feedback">You must accept before submitting.</div>
</div>
</div>
<div class="col-12"><button class="btn btn-primary" type="submit">Register</button></div>
</form>
<script>
document.querySelectorAll('.needs-validation').forEach((form) => {
form.addEventListener('submit', (event) => {
event.preventDefault(); // stay on the page in this demo
if (form.checkValidity()) {
console.log('form is valid — would be sent now');
} else {
console.log('form has errors');
}
form.classList.add('was-validated');
});
});
</script>
</body>
</html>Click Register with empty fields, then fix them one by one — the styles update as you type, because .was-validated keeps watching :valid/:invalid.
Note: In a real form you'd only call
event.preventDefault()whencheckValidity()is false, so a valid form is submitted normally. And always validate again on the server — client-side checks can be bypassed.
Useful HTML validation attributes
| Attribute | Rule |
|---|---|
required |
Must not be empty (checkbox must be checked) |
type="email", type="url", type="number" |
Must match the type |
minlength / maxlength |
Text length limits |
min / max / step |
Number or date limits |
pattern="regex" |
Must match a regular expression |
Manual .is-valid / .is-invalid
When the rule is something HTML can't express — "passwords must match", "username already taken" — set the classes yourself.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<div class="mb-2">
<label for="pw1" class="form-label">Password</label>
<input type="password" class="form-control" id="pw1">
</div>
<div class="mb-2">
<label for="pw2" class="form-label">Repeat password</label>
<input type="password" class="form-control" id="pw2">
<div class="valid-feedback">Passwords match ✔</div>
<div class="invalid-feedback">Passwords don't match.</div>
</div>
<script>
const pw1 = document.getElementById('pw1');
const pw2 = document.getElementById('pw2');
function check() {
if (!pw2.value) { pw2.classList.remove('is-valid', 'is-invalid'); return; }
const ok = pw1.value === pw2.value;
pw2.classList.toggle('is-valid', ok);
pw2.classList.toggle('is-invalid', !ok);
}
pw1.addEventListener('input', check);
pw2.addEventListener('input', check);
</script>
</body>
</html>Tooltip-style feedback and input groups
Swap .invalid-feedback for .invalid-tooltip (and .valid-tooltip) to show the message as a floating label. The parent needs position-relative. Inside an input group, add .has-validation to the group so the rounded corners stay correct.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
<form class="was-validated" novalidate>
<div class="position-relative mb-5">
<label for="age" class="form-label">Age (18–99)</label>
<input type="number" class="form-control" id="age" min="18" max="99" value="12" required>
<div class="invalid-tooltip">Please enter an age between 18 and 99.</div>
</div>
<label for="handle" class="form-label">Handle</label>
<div class="input-group has-validation">
<span class="input-group-text">@</span>
<input class="form-control" id="handle" required pattern="[a-z0-9_]+" value="Bad Name!">
<div class="invalid-feedback">Lowercase letters, digits and _ only.</div>
</div>
</form>
</body>
</html>This form starts with .was-validated, so the errors are visible immediately. Fix the values to see them turn green.
Class reference
| Class | Purpose |
|---|---|
.was-validated (on <form>) |
Style all fields by their :valid/:invalid state |
.needs-validation |
Just a hook for your script (no styles) |
.is-valid / .is-invalid |
Force a field's state manually |
.valid-feedback / .invalid-feedback |
Message shown in that state |
.valid-tooltip / .invalid-tooltip |
Message as a tooltip (parent position-relative) |
.has-validation |
Fixes input-group corners with feedback |
Tip: Write helpful messages: say what's expected ("At least 8 characters") rather than just "Invalid".
Test yourself
Why add novalidate to a Bootstrap-validated form?
- To disable all validation
- To stop the browser's own error bubbles so Bootstrap's feedback shows instead
- It's required for .form-control
- To validate on the server
Which class on the form makes Bootstrap show valid/invalid styles for every field?
- .needs-validation
- .was-validated
- .validated
- .form-validate
Where must an .invalid-feedback element be placed?
- Anywhere in the form
- Right after the control (as a sibling)
- Inside the label
- Before the control