Bootstrap Validation Examples

Ruth Jul 09, 2026

Bootstrap, the popular front-end framework, offers a wide range of pre-built components and utilities to streamline web development. Among its many features, Bootstrap validation is a crucial aspect that ensures user input is accurate and reliable. In this article, we'll explore various Bootstrap validation examples, helping you understand and implement these features effectively.

HTML Form Validation with Bootstrap
HTML Form Validation with Bootstrap

Before diving into specific examples, let's briefly discuss why validation is essential. Validation helps maintain data integrity, enhances user experience by providing real-time feedback, and improves the overall security of your web application. Now, let's explore Bootstrap's validation options.

Bootstrap Forms
Bootstrap Forms

Built-in Bootstrap Validation

Bootstrap 5 comes with built-in form validation features, making it easy to add basic validation to your forms. These features include feedback icons, tooltips, and customizable validation styles.

Bootstrap Fieldset Legend: How To + Examples + Template | AZMIND
Bootstrap Fieldset Legend: How To + Examples + Template | AZMIND

To use built-in validation, add the `was-validated` class to your form, and the `is-valid` or `is-invalid` classes to your input fields based on their validation status. Bootstrap will automatically display the appropriate feedback icons.

Basic Validation Examples

Bootstrap Modal Popup Form Submit with Ajax & PHP - CodexWorld
Bootstrap Modal Popup Form Submit with Ajax & PHP - CodexWorld

Here's a simple example of a form with built-in Bootstrap validation:

<form class="needs-validation" novalidate>
  <div class="mb-3">
    <label for="email" class="form-label">Email</label>
    <input type="email" class="form-control" id="email" required>
    <div class="invalid-feedback">Please enter a valid email address.</div>
  </div>
  <button class="btn btn-primary" type="submit">Submit</button>
</form>

In this example, the `required` attribute is used to mark the email field as mandatory. The `invalid-feedback` element displays a custom error message when the field is invalid.

Validation States

Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5
Bootstrap Tutorial | Bootstrap Notes | Bootstrap 5

Bootstrap provides three validation states: `is-valid`, `is-invalid`, and `is-empty`. You can use these classes to style your input fields based on their validation status. Here's an example:

<input type="text" class="form-control</input>
<input type="text" class="form-control is-valid</input>
<input type="text" class="form-control is-invalid</input>

These classes will apply the appropriate styles to your input fields, providing visual feedback to users.

Custom Validation with JavaScript

Free Bootstrap 4 Login Form
Free Bootstrap 4 Login Form

While built-in validation covers basic use cases, you might need to implement custom validation logic for more complex scenarios. Bootstrap provides JavaScript plugins to help with this.

To use the validation plugin, include the JavaScript and CSS files in your project, and initialize the plugin on your form. Here's an example:

A Beginner's Guide to the Latest Bootstrap 5 Utilities
A Beginner's Guide to the Latest Bootstrap 5 Utilities
Bootstrap Snippets Library & Layout Examples [Code Demos] | BootstrapCreative
Bootstrap Snippets Library & Layout Examples [Code Demos] | BootstrapCreative
BOOTSTRAP INPUT FORM (2020) | HOW TO MAKE FORM IN BOOTSTRAP
BOOTSTRAP INPUT FORM (2020) | HOW TO MAKE FORM IN BOOTSTRAP
the react bootstop form validation screen is shown with an icon for react bots
the react bootstop form validation screen is shown with an icon for react bots
How to use real-time validation in your forms
How to use real-time validation in your forms
Form validation best practices
Form validation best practices
Robert-Personal Portfolio HTML Template: Modern & Responsive Design for Creatives
Robert-Personal Portfolio HTML Template: Modern & Responsive Design for Creatives
Landing Page Template: Responsive Personal Portfolio with Light & Dark Modes
Landing Page Template: Responsive Personal Portfolio with Light & Dark Modes
A Complete Guide To Live Validation UX — Smashing Magazine
A Complete Guide To Live Validation UX — Smashing Magazine
The Ultimate UX Design of Form Validation
The Ultimate UX Design of Form Validation
an image of a website page with the wordpress plugs highlighted in blue and white
an image of a website page with the wordpress plugs highlighted in blue and white
Pangong - Developer-friendly Bootstrap Admin Dashboard + UI Kit
Pangong - Developer-friendly Bootstrap Admin Dashboard + UI Kit
Rukada - Responsive Bootstrap 5 Admin Template
Rukada - Responsive Bootstrap 5 Admin Template
Ultimate Bootstrap Forms
Ultimate Bootstrap Forms
Rocker - Bootstrap 5 Admin Dashboard Template
Rocker - Bootstrap 5 Admin Dashboard Template
Blog Website HTML Template: Responsive Bootstrap 4 Theme with Light & Dark Modes
Blog Website HTML Template: Responsive Bootstrap 4 Theme with Light & Dark Modes
The Ultimate UX Design of Form Validation
The Ultimate UX Design of Form Validation
Why Users Make More Errors with Instant Inline Validation
Why Users Make More Errors with Instant Inline Validation
a screenshot of google's dashboard with the search bar highlighted in red and blue
a screenshot of google's dashboard with the search bar highlighted in red and blue
an image of a web page with the number of people on it
an image of a web page with the number of people on it

Initializing the Validation Plugin

First, include the Bootstrap CSS and JavaScript files in your project. Then, add the following script to initialize the validation plugin:

<script>
  (function () {
    'use strict'

    // Fetch all the forms we want to apply custom Bootstrap validation styles to
    var forms = document.querySelectorAll('.needs-validation')

    // Loop over them and prevent submission
    Array.prototype.slice.call(forms)
      .forEach(function (form) {
        form.addEventListener('submit', function (event) {
          if (!form.checkValidity()) {
            event.preventDefault()
            event.stopPropagation()
          }

          form.classList.add('was-validated')
        }, false)
      })
  })()
</script>

In this example, the script loops through all forms with the `needs-validation` class and adds the `was-validated` class when the form is submitted. This triggers Bootstrap's built-in validation styles.

Custom Validation Examples

Now that we've initialized the validation plugin, let's explore some custom validation examples.

**Password Matching:** To ensure that the user enters the same password in both fields, you can use the `pattern` attribute and a regular expression to compare the values:

<div class="mb-3">
  <label for="password" class="form-label">Password</label>
  <input type="password" class="form-control" id="password" required>
</div>
<div class="mb-3">
  <label for="confirm-password" class="form-label">Confirm Password</label>
  <input type="password" class="form-control" id="confirm-password" required pattern="password">
  <div class="invalid-feedback">Passwords do not match</div>
</div>

In this example, the `pattern` attribute on the confirm password field compares its value to the password field. If the values don't match, Bootstrap will display the custom error message.

As we've seen, Bootstrap offers a wide range of validation options to help you create reliable and user-friendly forms. By leveraging built-in validation features and custom JavaScript plugins, you can ensure that user input is accurate and reliable. So go ahead, start implementing Bootstrap validation in your projects, and watch your forms shine!