Bootstrap form validation uses built-in styles to show valid/invalid states with visual feedback. 1. Add novalidate to disable browser tooltips. 2. Use needs-validation for real-time feedback or was-validated after submission. 3. Include HTML5 validation attributes like required. 4. Display error messages in invalid-feedback elements. 5. Apply was-validated via JavaScript on form submission to trigger styling. 6. Optionally, use JavaScript to manually add is-valid or is-invalid for custom validation. Always pair with backend validation for security, use semantic input types, and ensure accessibility. This approach ensures clean, responsive, and accessible forms.
Bootstrap form validation is a built-in feature in Bootstrap 5 that helps you provide visual feedback when users fill out forms incorrectly or correctly. It works best with HTML5 form validation attributes (like required
, pattern
, etc.) and integrates well with JavaScript for custom logic.

Here’s how to use Bootstrap form validation properly:
? Enable Bootstrap Validation Styles
Bootstrap provides two states:

- Invalid (red border and icon) — when input fails validation
- Valid (green border and checkmark) — when input passes
These styles are applied automatically when you use the was-validated
class on the form and :valid
/ :invalid
pseudo-classes on inputs.
Step 1: Add novalidate
to the form
This disables the browser's default popup tooltips and lets Bootstrap handle the styling.

<form class="row g-3 needs-validation" novalidate>
? Use
needs-validation
if you want real-time feedback without submitting. Usewas-validated
after submission to show errors.
? HTML Structure with Validation
<form class="row g-3 needs-validation" novalidate>Please enter your first name.Please provide a valid email.
? The
invalid-feedback
message only appears when the field is invalid and the form has been submitted (or marked as validated). ? Always includerequired
,minlength
, or other validation attributes.
? When to Show Errors: Use was-validated
To show validation styles only after the user tries to submit, add the was-validated
class via JavaScript.
const form = document.querySelector('form'); form.addEventListener('submit', function (event) { if (!form.checkValidity()) { event.preventDefault(); event.stopPropagation(); } form.classList.add('was-validated'); });
Now:
- On submit, if any field fails, the browser prevents submission.
- The
was-validated
class triggers Bootstrap’s error styling. invalid-feedback
messages become visible.
? Optional: Real-Time Validation (without was-validated
)
If you want instant feedback as the user types, remove was-validated
and use needs-validation
. You can manually trigger validation on input:
document.querySelectorAll('.form-control').forEach(input => { input.addEventListener('input', () => { if (input.checkValidity()) { input.classList.remove('is-invalid'); input.classList.add('is-valid'); } else { input.classList.remove('is-valid'); input.classList.add('is-invalid'); } }); });
Then use is-valid
and is-invalid
classes manually or rely on CSS pseudo-classes.
? Tips & Best Practices
- Always include accessible error messages inside
invalid-feedback
orvalid-feedback
. - Use semantic HTML5 input types (
email
,tel
,url
, etc.) for better client-side validation. - Test on mobile — some validations behave differently.
- Combine with backend validation — never rely solely on frontend checks.
? Example: Custom JS Validation with Bootstrap Styling
const form = document.getElementById('myForm'); form.addEventListener('submit', function (e) { e.preventDefault(); // Custom logic const email = document.getElementById('email'); const emailValue = email.value; if (!emailValue.includes('@')) { email.classList.remove('is-valid'); email.classList.add('is-invalid'); } else { email.classList.remove('is-invalid'); email.classList.add('is-valid'); } // Only submit if all fields are valid if (form.checkValidity() && email.classList.contains('is-valid')) { alert('Form submitted!'); form.reset(); form.classList.remove('was-validated'); } else { form.classList.add('was-validated'); } });
Basically, Bootstrap handles the visuals — you just need to:
- Use
required
or other validation attributes - Add
novalidate
to avoid browser popups - Use
was-validated
after submission - Show
invalid-feedback
messages
That’s it. Clean, accessible, and responsive.
The above is the detailed content of How to use Bootstrap form validation?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

How to use middleware to handle form validation in Laravel, specific code examples are required Introduction: Form validation is a very common task in Laravel. In order to ensure the validity and security of the data entered by users, we usually verify the data submitted in the form. Laravel provides a convenient form validation function and also supports the use of middleware to handle form validation. This article will introduce in detail how to use middleware to handle form validation in Laravel and provide specific code examples.

PHP is a very popular programming language, and CodeIgniter4 is a commonly used PHP framework. When developing web applications, using frameworks is very helpful. It can speed up the development process, improve code quality, and reduce maintenance costs. This article will introduce how to use the CodeIgniter4 framework. Installing the CodeIgniter4 framework The CodeIgniter4 framework can be downloaded from the official website (https://codeigniter.com/). Down

PHP is a scripting language widely used in web development, and its form validation and filtering are very important parts. When the user submits the form, the data entered by the user needs to be verified and filtered to ensure the security and validity of the data. This article will introduce methods and techniques on how to perform form validation and filtering in PHP. 1. Form validation Form validation refers to checking the data entered by the user to ensure that the data complies with specific rules and requirements. Common form verification includes verification of required fields, email format, and mobile phone number format.

How to use Flask-WTF to implement form validation Flask-WTF is a Flask extension for handling web form validation. It provides a concise and flexible way to validate user-submitted data. This article will show you how to use the Flask-WTF extension to implement form validation. Install Flask-WTF To use Flask-WTF, you first need to install it. You can use the pip command to install: pipinstallFlask-WTF import the required modules in F

Form validation is a very important link in web application development. It can check the validity of the data before submitting the form data to avoid security vulnerabilities and data errors in the application. Form validation for web applications can be easily implemented using Golang. This article will introduce how to use Golang to implement form validation for web applications. 1. Basic elements of form validation Before introducing how to implement form validation, we need to know what the basic elements of form validation are. Form elements: form elements are

Laravel is a popular PHP web development framework that provides many convenient features to speed up the work of developers. Among them, LaravelValidation is a very practical function that can help us easily validate form requests and user-entered data. This article will introduce how to use LaravelValidation to validate form requests. What is LaravelValidationLaravelValidation is La

PHP form validation tips: How to use the filter_input function to verify user input Introduction: When developing web applications, forms are an important tool for interacting with users. Correctly validating user input is one of the key steps to ensure data integrity and security. PHP provides the filter_input function, which can easily verify and filter user input. This article will introduce how to use the filter_input function to verify user input and provide relevant code examples. one,

In web development, form validation is an extremely critical part. Form verification can effectively protect data security and prevent attacks and malicious operations by illegal users. In Golang, form validation technology is also widely used, especially in web applications. This article will introduce the practice of form validation for web applications in Golang. 1. Basic Principles of Form Validation In web applications, the basic principle of form validation is to check and verify data before submitting data on the web page. This data may be user
