Validation

Source: https://getbootstrap.com/docs/6.0/forms/validation

Provide valuable, actionable feedback to your users with HTML5 form validation, via browser default behaviors or custom styles and JavaScript.

How it works

Here’s how form validation works with Bootstrap:

  • Client-side validation uses CSS pseudo-classes scoped behind the data-bs-validate attribute. Add data-bs-validate to your <form> to opt in to :user-invalid styling—validation styles only appear after the user interacts with a control (e.g., typing and blurring, or attempting to submit), so required fields don’t show up as invalid on page load.
  • By default, client-side validation only shows invalid styling. To also show success styling for valid fields, set data-bs-validate="valid".
  • Server-side validation uses the .is-invalid and .is-valid classes to indicate field state without requiring user interaction. These work globally—no data-bs-validate attribute needed.
  • Custom validation states beyond valid/invalid (e.g., warning) are supported via the .is-* class pattern and the $validation-states Sass map. There are no generated pseudo-classes for custom states.
  • All modern browsers support the constraint validation API, a series of JavaScript methods for validating form controls.
  • Feedback messages may utilize the browser defaults (different for each browser, and unstylable via CSS) or our custom feedback styles with additional HTML and CSS.
  • You may provide custom validity messages with setCustomValidity in JavaScript.

With that in mind, consider the following demos for our custom form validation styles, optional server-side classes, and browser defaults.

Custom styles

For custom Bootstrap form validation messages, add data-bs-validate and the novalidate boolean attribute to your <form>. The data-bs-validate attribute opts in to Bootstrap’s :user-invalid styling, while novalidate disables the browser default feedback tooltips. Custom feedback styles apply custom colors, borders, and focus styles to better communicate feedback.

Use data-bs-validate="valid" to also show success styling on valid fields. Add required to each form input and .invalid-feedback to provide field-specific error messages. If you enable valid styling, use .valid-feedback for success messages.

For accessibility, add aria-describedby to each form control, pointing to the id of its feedback message. This ensures screen readers announce the error when the user focuses an invalid field. The example JavaScript below also sets aria-invalid on each control at submit time and clears it as users correct their input.

For example, try to submit the form below; our JavaScript will intercept the submit button and relay feedback to you.

<Example code={`<form class="grid grid-cols-1 gap-5" data-bs-validate novalidate><!-- [!code highlight] --> <div class="grid gap-5"> <div class="form-field md:g-col-6"> <label for="validationCustom01" class="form-label">Name</label> <input type="text" class="form-control" id="validationCustom01" value="Mark Otto" required aria-describedby="validationCustom01Feedback"><!-- [!code highlight] --> <div id="validationCustom01Feedback" class="invalid-feedback">Full name is required</div><!-- [!code highlight] --> </div> <div class="form-field md:g-col-6"> <label for="validationCustomUsername" class="form-label">Donation amount</label> <div class="input-group"> <span class="input-group-text" id="inputGroupPrepend">$</span> <input type="number" class="form-control" value="100" id="validationCustomUsername" aria-describedby="inputGroupPrepend validationCustomUsernameFeedback" required><!-- [!code highlight] --> </div> <div id="validationCustomUsernameFeedback" class="invalid-feedback">Please enter a donation amount</div><!-- [!code highlight] --> </div> </div>

<div class="form-field"> <label for="validationCustomAddress" class="form-label">Address</label> <input type="text" class="form-control" id="validationCustomAddress" value="1234 Main St" required aria-describedby="validationCustomAddressFeedback"><!-- [!code highlight] --> <div id="validationCustomAddressFeedback" class="invalid-feedback">Please enter a mailing address</div><!-- [!code highlight] --> </div>

<div class="grid gap-5"> <div class="form-field md:g-col-6"> <label for="validationCustom03" class="form-label">City</label> <input type="text" class="form-control" id="validationCustom03" required aria-describedby="validationCustom03Feedback"><!-- [!code highlight] --> <div id="validationCustom03Feedback" class="invalid-feedback">Please provide a city</div><!-- [!code highlight] --> </div> <div class="form-field md:g-col-4"> <label for="validationCustom04" class="form-label">State</label> <select class="form-control" id="validationCustom04" required aria-describedby="validationCustom04Feedback"><!-- [!code highlight] --> <option selected disabled value="">Choose…</option> <option>...</option> </select> <div id="validationCustom04Feedback" class="invalid-feedback">Please select a state</div><!-- [!code highlight] --> </div> <div class="form-field md:g-col-2"> <label for="validationCustom05" class="form-label">Zip</label> <input type="text" class="form-control" id="validationCustom05" required aria-describedby="validationCustom05Feedback"><!-- [!code highlight] --> <div id="validationCustom05Feedback" class="invalid-feedback">Required</div><!-- [!code highlight] --> </div> </div>

<div class="form-group"> <label class="form-label">Shipping method</label> <div class="form-field mb-1"> <input type="radio" id="validationCustomRadio1" class="radio" name="shipping" required><!-- [!code highlight] --> <div class="form-field-content"> <label for="validationCustomRadio1">Standard</label> <small class="form-text">Arrives in 5-7 business days.</small> </div> </div> <div class="form-field"> <input type="radio" id="validationCustomRadio2" class="radio" name="shipping" required><!-- [!code highlight] --> <div class="form-field-content"> <label for="validationCustomRadio2">Express</label> <small class="form-text">Arrives in 2-3 business days.</small> </div> </div> </div>

<div class="form-field"> <div class="switch"> <input type="checkbox" id="validationCustomSwitch" role="switch" switch> </div> <label for="validationCustomSwitch">Subscribe to newsletter</label> </div>

<button class="btn-solid theme-primary justify-self-start" type="submit">Submit form</button> </form>`} />

Browser defaults

Not interested in custom validation feedback messages or writing JavaScript to change form behaviors? All good, you can use the browser defaults. Try submitting the form below. Depending on your browser and OS, you’ll see a slightly different style of feedback.

While these feedback styles cannot be styled with CSS, you can still customize the feedback text through JavaScript.

<div class="grid gap-5"> <div class="form-field md:g-col-6"> <label for="validationDefault01" class="form-label">Name</label> <input type="text" class="form-control" id="validationDefault01" value="Mark Otto" required> </div> <div class="form-field md:g-col-6"> <label for="validationDefaultDonation" class="form-label">Donation amount</label> <div class="input-group"> <span class="input-group-text" id="inputGroupPrepend2">$</span> <input type="number" class="form-control" value="100" id="validationDefaultDonation" aria-describedby="inputGroupPrepend2" required> </div> </div> </div>

<div class="form-field"> <label for="validationDefaultAddress" class="form-label">Address</label> <input type="text" class="form-control" id="validationDefaultAddress" value="1234 Main St" required> </div>

<div class="grid gap-5"> <div class="form-field md:g-col-6"> <label for="validationDefault03" class="form-label">City</label> <input type="text" class="form-control" id="validationDefault03" required> </div> <div class="form-field md:g-col-4"> <label for="validationDefault04" class="form-label">State</label> <select class="form-control" id="validationDefault04" required> <option selected disabled value="">Choose…</option> <option>...</option> </select> </div> <div class="form-field md:g-col-2"> <label for="validationDefault05" class="form-label">Zip</label> <input type="text" class="form-control" id="validationDefault05" required> </div> </div>

<div class="form-group"> <label class="form-label">Shipping method</label> <div class="form-field mb-1"> <input type="radio" id="validationDefaultRadio1" class="radio" name="shippingDefault" required> <div class="form-field-content"> <label for="validationDefaultRadio1">Standard</label> <small class="form-text">Arrives in 5-7 business days.</small> </div> </div> <div class="form-field"> <input type="radio" id="validationDefaultRadio2" class="radio" name="shippingDefault" required> <div class="form-field-content"> <label for="validationDefaultRadio2">Express</label> <small class="form-text">Arrives in 2-3 business days.</small> </div> </div> </div>

<div class="form-field"> <div class="switch"> <input type="checkbox" id="validationDefaultSwitch" role="switch" switch> </div> <label for="validationDefaultSwitch">Subscribe to newsletter</label> </div>

<button class="btn-solid theme-primary justify-self-start" type="submit">Submit form</button> </form>`} />

Server-side

We recommend using client-side validation, but in case you require server-side validation, you can indicate invalid and valid form fields with .is-invalid and .is-valid. Note that .invalid-feedback is also supported with these classes.

For invalid fields, ensure that the invalid feedback/error message is associated with the relevant form field using aria-describedby (noting that this attribute allows more than one id to be referenced, in case the field already points to additional form text).

<div class="grid gap-5"> <div class="form-field md:g-col-6"> <label for="validationServer01" class="form-label">Name</label> <input type="text" class="form-control is-valid" id="validationServer01" value="Mark Otto" aria-describedby="validationServer01Valid validationServer01Invalid"><!-- [!code highlight] --> <div id="validationServer01Valid" class="valid-feedback">Looks great!</div><!-- [!code highlight] --> <div id="validationServer01Invalid" class="invalid-feedback">Full name is required</div><!-- [!code highlight] --> </div> <div class="form-field md:g-col-6"> <label for="validationServerDonation" class="form-label">Donation amount</label> <div class="input-group"> <span class="input-group-text" id="inputGroupPrepend3">$</span> <input type="number" class="form-control is-invalid" value="" id="validationServerDonation" aria-describedby="inputGroupPrepend3 serverDonateFeedbackValid serverDonateFeedbackInvalid"><!-- [!code highlight] --> </div> <div id="serverDonateFeedbackValid" class="valid-feedback">Looks great!</div><!-- [!code highlight] --> <div id="serverDonateFeedbackInvalid" class="invalid-feedback">Please enter a donation amount</div><!-- [!code highlight] --> </div> </div>

<div class="form-field"> <label for="validationServerAddress" class="form-label">Address</label> <input type="text" class="form-control is-valid" id="validationServerAddress" value="1234 Main St" aria-describedby="validationServerAddressValid validationServerAddressInvalid"><!-- [!code highlight] --> <div id="validationServerAddressValid" class="valid-feedback">Looks great!</div><!-- [!code highlight] --> <div id="validationServerAddressInvalid" class="invalid-feedback">Please enter a mailing address</div> </div>

<div class="grid gap-5"> <div class="form-field md:g-col-6"> <label for="validationServer03" class="form-label">City</label> <input type="text" class="form-control is-invalid" id="validationServer03" aria-describedby="validationServer03Feedback"><!-- [!code highlight] --> <div id="validationServer03Feedback" class="invalid-feedback">Please provide a city</div> </div> <div class="form-field md:g-col-4"> <label for="validationServer04" class="form-label">State</label> <select class="form-control is-invalid" id="validationServer04" aria-describedby="validationServer04Feedback"><!-- [!code highlight] --> <option selected disabled value="">Choose…</option> <option>...</option> </select> <div id="validationServer04Feedback" class="invalid-feedback">Please select a state</div> </div> <div class="form-field md:g-col-2"> <label for="validationServer05" class="form-label">Zip</label> <input type="text" class="form-control is-invalid" id="validationServer05" aria-describedby="validationServer05Feedback"><!-- [!code highlight] --> <div id="validationServer05Feedback" class="invalid-feedback">Required</div> </div> </div>