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.

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.

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.

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

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 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

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:

![Bootstrap Snippets Library & Layout Examples [Code Demos] | BootstrapCreative](https://i.pinimg.com/originals/35/22/dd/3522dd92050cce928b0415829bb7e0e2.jpg)


















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!