splitforms.com
guide · form validation

Bootstrap Form Validation — Native HTML + Bootstrap 5

Last reviewed

Bootstrap 5 ships form validation as a thin layer on top of native HTML — no jQuery, no plugin, no JavaScript validation library. Just required attributes, .was-validated class, and .invalid-feedback divs.

html
<form class="needs-validation" novalidate
      action="https://splitforms.com/api/submit" method="POST">
  <input type="hidden" name="access_key" value="YOUR_ACCESS_KEY" />

  <div class="mb-3">
    <label class="form-label">Name</label>
    <input name="name" type="text" class="form-control" required minlength="2" />
    <div class="invalid-feedback">Please enter your name (at least 2 chars).</div>
  </div>

  <div class="mb-3">
    <label class="form-label">Email</label>
    <input name="email" type="email" class="form-control" required />
    <div class="invalid-feedback">Valid email required.</div>
  </div>

  <div class="mb-3">
    <label class="form-label">Message</label>
    <textarea name="message" class="form-control" rows="4" required></textarea>
    <div class="invalid-feedback">Please write a message.</div>
  </div>

  <button type="submit" class="btn btn-primary">Send</button>
</form>

<script>
(() => {
  const forms = document.querySelectorAll(".needs-validation");
  Array.from(forms).forEach(form => {
    form.addEventListener("submit", e => {
      if (!form.checkValidity()) { e.preventDefault(); e.stopPropagation(); }
      form.classList.add("was-validated");
    }, false);
  });
})();
</script>

Bootstrap 5 dropped jQuery validation in favor of native HTML validation styled with Bootstrap classes. The pattern: add novalidate to the form (disables the default browser UI), add required/pattern/type=email to each input, add .invalid-feedback divs for error messages, and toggle .was-validated on submit attempts.

The native browser validation runs whenever the user submits. If validation fails, the browser blocks the submit; you add the .was-validated class which triggers Bootstrap's styled error UI (red borders on invalid inputs, .invalid-feedback divs become visible). Tab through the form and the error states cascade naturally.

For custom validation rules beyond what HTML offers (cross-field, async, complex patterns), use setCustomValidity on the input. The error message is whatever string you pass; Bootstrap displays it the same way as built-in errors. ~5 lines for password confirmation, no jQuery needed.

Bootstrap 5's approach is cleaner than the jQuery-validate era because it leans on the platform: native HTML attributes for the rules, native validation for the logic, CSS for the visual feedback. The JavaScript glue (~10 lines) just controls when to add the .was-validated class. Most forms need no custom JS at all.

How to set this up

Step 01

Add novalidate to the form

Disables the default browser validation UI; Bootstrap takes over the styling.

Step 02

Use native validation attributes

required, minlength, pattern, type=email — these are the rules. No library code.

Step 03

Add .invalid-feedback divs

One per field, containing the error message. Hidden until .was-validated triggers them.

Step 04

Toggle .was-validated on submit

10 lines of JS: on submit, check validity; if invalid, prevent submission and add .was-validated class to trigger the error UI.

Native HTML attributes, ~10 lines of glue JS, no library.

Frequently asked questions

Does Bootstrap 5 still need jQuery for validation?

No. Bootstrap 5 dropped jQuery entirely. Form validation uses native HTML attributes and ~10 lines of vanilla JS for the .was-validated toggle.

How do I show custom error messages in Bootstrap?

Place the error message in a <div class="invalid-feedback"> element immediately after the input. It's hidden by default; .was-validated on the parent form reveals it for invalid inputs.

How do I do password confirmation in Bootstrap validation?

Use setCustomValidity on the confirm input via JavaScript: on input event, set it to '' if matching, or 'Passwords don't match' if not. Bootstrap displays the custom message the same way as built-in errors.

Can I use server-side validation with Bootstrap?

Yes — Bootstrap's .is-invalid class applied programmatically (e.g., from a server response) shows the error styling without triggering native validation. Useful for displaying errors returned by your backend.

What if a user has JavaScript disabled?

Native HTML validation still fires (the browser blocks submissions with empty required fields). The Bootstrap styling won't fully render — error divs stay hidden — but the form still rejects invalid input. Progressive enhancement works correctly.

Related guides

Ship the form, not the backend.

Free for 500 submissions total. Owner email delivery, spam filtering, and a real dashboard are included on Free. Paid plans add signed webhooks. No credit card.

Get a free access key →