Bootstrap 5·Bootstrap from grid to dashboard

Forms and validation

Build labelled, accessible forms and wire Bootstrap validation states.

Beginner 14 min read Lesson 4 of 300 Challenge included
What you will be able to do
  • Use form-label and an explicit for/id pair.
  • Use form-control, form-select and form-check.
  • Add invalid-feedback next to the field.
  • Keep server-side validation regardless of client hints.

In plain English

Every control needs a label. Bootstrap styles the control, but the label association and the error text are your responsibility. Use invalid-feedback inside a wrapper and add .was-validated on submit or the .is-invalid class per field.

form.htmlHTML
<form class="row g-3 needs-validation" novalidate>
  <div class="col-md-6">
    <label class="form-label" for="email">Email</label>
    <input class="form-control" id="email" type="email" required>
    <div class="invalid-feedback">Enter a valid email.</div>
  </div>
  <div class="col-12">
    <button class="btn btn-primary" type="submit">Subscribe</button>
  </div>
</form>
Your turn

Add a required name field

Easy

Add a labelled name input with required validation and an invalid-feedback message.

HTML · starter
<div class="col-md-6">
Check your understanding before moving on.
Knowledge checkQuestion 1

What does the invalid-feedback class do?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Next: Buttons and badges
Lesson 4 of 300 · 0% of course