Class 17 of 48 • Forms & Inputs

Bootstrap 5 Form Validation: Client-Side States & Feedback

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.

How Bootstrap 5 Validation Operates

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.

Bootstrap 5 Form Validation States: Valid vs Invalid Feedback
Figure 5: Visual feedback differences between .valid-feedback and .invalid-feedback states.

Complete Compilable Code Example

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>

Server-Side Validation: .is-valid & .is-invalid

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.

Explore Related Bootstrap Tutorials & Next Steps

Strengthen your responsive web design mastery by studying these connected classes:

Core Form Controls

Understand base input elements, required attributes, and form structure.

Validating Floating Labels

Combine floating label animations with .is-valid and .is-invalid.

Validation Tooltip Badges

Display error messages as floating bubbles with .invalid-tooltip.

Handling Form Submit Events

Prevent default form submission in JavaScript until inputs pass validation.

Frequently Asked Questions (FAQ) - Student & Interview Review

The 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.
Replace .invalid-feedback with .invalid-tooltip. Ensure the parent container has position: relative; so the tooltip positions correctly.
Yes! Use the HTML5 pattern attribute on the password input (e.g. pattern='(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{8,}'), and display detailed requirements inside the .invalid-feedback div.

Next Step in Your Bootstrap Learning Path:

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

Share Share on Facebook Share on Twitter Share on LinkedIn Pin on Pinterest Share on Reddit

You may also like this!