Validation

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

We are aware that currently the client-side custom validation styles and tooltips are not accessible, since they are not exposed to assistive technologies. While we work on a solution, we’d recommend either using the server-side option or the default browser validation method.

How it works

Here’s how form validation works with Bootstrap:

  • HTML form validation is applied via CSS’s two pseudo-classes, :invalid and :valid. It applies to <input>, <select>, and <textarea> elements.
  • Bootstrap scopes the :invalid and :valid styles to parent .was-validated class, usually applied to the <form>. Otherwise, any required field without a value shows up as invalid on page load. This way, you may choose when to activate them (typically after form submission is attempted).
  • To reset the appearance of the form (for instance, in the case of dynamic form submissions using Ajax), remove the .was-validated class from the <form> again after submission.
  • As a fallback, .is-invalid and .is-valid classes may be used instead of the pseudo-classes for server-side validation. They do not require a .was-validated parent class.
  • Due to constraints in how CSS works, we cannot (at present) apply styles to a <label> that comes before a form control in the DOM without the help of custom JavaScript.
  • 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, you’ll need to add the novalidate boolean attribute to your <form>. This disables the browser default feedback tooltips, but still provides access to the form validation APIs in JavaScript. Try to submit the form below; our JavaScript will intercept the submit button and relay feedback to you. When attempting to submit, you’ll see the :invalid and :valid styles applied to your form controls.

Custom feedback styles apply custom colors, borders, focus styles, and background icons to better communicate feedback. Background icons for <select>s are only available with .form-select, and not .form-control.

Validation - 图1

html

  1. <form class="row g-3 needs-validation" novalidate>
  2. <div class="col-md-4">
  3. <label for="validationCustom01" class="form-label">First name</label>
  4. <input type="text" class="form-control" id="validationCustom01" value="Mark" required>
  5. <div class="valid-feedback">
  6. Looks good!
  7. </div>
  8. </div>
  9. <div class="col-md-4">
  10. <label for="validationCustom02" class="form-label">Last name</label>
  11. <input type="text" class="form-control" id="validationCustom02" value="Otto" required>
  12. <div class="valid-feedback">
  13. Looks good!
  14. </div>
  15. </div>
  16. <div class="col-md-4">
  17. <label for="validationCustomUsername" class="form-label">Username</label>
  18. <div class="input-group has-validation">
  19. <span class="input-group-text" id="inputGroupPrepend">@</span>
  20. <input type="text" class="form-control" id="validationCustomUsername" aria-describedby="inputGroupPrepend" required>
  21. <div class="invalid-feedback">
  22. Please choose a username.
  23. </div>
  24. </div>
  25. </div>
  26. <div class="col-md-6">
  27. <label for="validationCustom03" class="form-label">City</label>
  28. <input type="text" class="form-control" id="validationCustom03" required>
  29. <div class="invalid-feedback">
  30. Please provide a valid city.
  31. </div>
  32. </div>
  33. <div class="col-md-3">
  34. <label for="validationCustom04" class="form-label">State</label>
  35. <select class="form-select" id="validationCustom04" required>
  36. <option selected disabled value="">Choose...</option>
  37. <option>...</option>
  38. </select>
  39. <div class="invalid-feedback">
  40. Please select a valid state.
  41. </div>
  42. </div>
  43. <div class="col-md-3">
  44. <label for="validationCustom05" class="form-label">Zip</label>
  45. <input type="text" class="form-control" id="validationCustom05" required>
  46. <div class="invalid-feedback">
  47. Please provide a valid zip.
  48. </div>
  49. </div>
  50. <div class="col-12">
  51. <div class="form-check">
  52. <input class="form-check-input" type="checkbox" value="" id="invalidCheck" required>
  53. <label class="form-check-label" for="invalidCheck">
  54. Agree to terms and conditions
  55. </label>
  56. <div class="invalid-feedback">
  57. You must agree before submitting.
  58. </div>
  59. </div>
  60. </div>
  61. <div class="col-12">
  62. <button class="btn btn-primary" type="submit">Submit form</button>
  63. </div>
  64. </form>
  1. // Example starter JavaScript for disabling form submissions if there are invalid fields
  2. (() => {
  3. 'use strict'
  4. // Fetch all the forms we want to apply custom Bootstrap validation styles to
  5. const forms = document.querySelectorAll('.needs-validation')
  6. // Loop over them and prevent submission
  7. Array.from(forms).forEach(form => {
  8. form.addEventListener('submit', event => {
  9. if (!form.checkValidity()) {
  10. event.preventDefault()
  11. event.stopPropagation()
  12. }
  13. form.classList.add('was-validated')
  14. }, false)
  15. })
  16. })()

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.

Validation - 图2

html

  1. <form class="row g-3">
  2. <div class="col-md-4">
  3. <label for="validationDefault01" class="form-label">First name</label>
  4. <input type="text" class="form-control" id="validationDefault01" value="Mark" required>
  5. </div>
  6. <div class="col-md-4">
  7. <label for="validationDefault02" class="form-label">Last name</label>
  8. <input type="text" class="form-control" id="validationDefault02" value="Otto" required>
  9. </div>
  10. <div class="col-md-4">
  11. <label for="validationDefaultUsername" class="form-label">Username</label>
  12. <div class="input-group">
  13. <span class="input-group-text" id="inputGroupPrepend2">@</span>
  14. <input type="text" class="form-control" id="validationDefaultUsername" aria-describedby="inputGroupPrepend2" required>
  15. </div>
  16. </div>
  17. <div class="col-md-6">
  18. <label for="validationDefault03" class="form-label">City</label>
  19. <input type="text" class="form-control" id="validationDefault03" required>
  20. </div>
  21. <div class="col-md-3">
  22. <label for="validationDefault04" class="form-label">State</label>
  23. <select class="form-select" id="validationDefault04" required>
  24. <option selected disabled value="">Choose...</option>
  25. <option>...</option>
  26. </select>
  27. </div>
  28. <div class="col-md-3">
  29. <label for="validationDefault05" class="form-label">Zip</label>
  30. <input type="text" class="form-control" id="validationDefault05" required>
  31. </div>
  32. <div class="col-12">
  33. <div class="form-check">
  34. <input class="form-check-input" type="checkbox" value="" id="invalidCheck2" required>
  35. <label class="form-check-label" for="invalidCheck2">
  36. Agree to terms and conditions
  37. </label>
  38. </div>
  39. </div>
  40. <div class="col-12">
  41. <button class="btn btn-primary" type="submit">Submit form</button>
  42. </div>
  43. </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).

To fix issues with border radius, input groups require an additional .has-validation class.

Validation - 图3

html

  1. <form class="row g-3">
  2. <div class="col-md-4">
  3. <label for="validationServer01" class="form-label">First name</label>
  4. <input type="text" class="form-control is-valid" id="validationServer01" value="Mark" required>
  5. <div class="valid-feedback">
  6. Looks good!
  7. </div>
  8. </div>
  9. <div class="col-md-4">
  10. <label for="validationServer02" class="form-label">Last name</label>
  11. <input type="text" class="form-control is-valid" id="validationServer02" value="Otto" required>
  12. <div class="valid-feedback">
  13. Looks good!
  14. </div>
  15. </div>
  16. <div class="col-md-4">
  17. <label for="validationServerUsername" class="form-label">Username</label>
  18. <div class="input-group has-validation">
  19. <span class="input-group-text" id="inputGroupPrepend3">@</span>
  20. <input type="text" class="form-control is-invalid" id="validationServerUsername" aria-describedby="inputGroupPrepend3 validationServerUsernameFeedback" required>
  21. <div id="validationServerUsernameFeedback" class="invalid-feedback">
  22. Please choose a username.
  23. </div>
  24. </div>
  25. </div>
  26. <div class="col-md-6">
  27. <label for="validationServer03" class="form-label">City</label>
  28. <input type="text" class="form-control is-invalid" id="validationServer03" aria-describedby="validationServer03Feedback" required>
  29. <div id="validationServer03Feedback" class="invalid-feedback">
  30. Please provide a valid city.
  31. </div>
  32. </div>
  33. <div class="col-md-3">
  34. <label for="validationServer04" class="form-label">State</label>
  35. <select class="form-select is-invalid" id="validationServer04" aria-describedby="validationServer04Feedback" required>
  36. <option selected disabled value="">Choose...</option>
  37. <option>...</option>
  38. </select>
  39. <div id="validationServer04Feedback" class="invalid-feedback">
  40. Please select a valid state.
  41. </div>
  42. </div>
  43. <div class="col-md-3">
  44. <label for="validationServer05" class="form-label">Zip</label>
  45. <input type="text" class="form-control is-invalid" id="validationServer05" aria-describedby="validationServer05Feedback" required>
  46. <div id="validationServer05Feedback" class="invalid-feedback">
  47. Please provide a valid zip.
  48. </div>
  49. </div>
  50. <div class="col-12">
  51. <div class="form-check">
  52. <input class="form-check-input is-invalid" type="checkbox" value="" id="invalidCheck3" aria-describedby="invalidCheck3Feedback" required>
  53. <label class="form-check-label" for="invalidCheck3">
  54. Agree to terms and conditions
  55. </label>
  56. <div id="invalidCheck3Feedback" class="invalid-feedback">
  57. You must agree before submitting.
  58. </div>
  59. </div>
  60. </div>
  61. <div class="col-12">
  62. <button class="btn btn-primary" type="submit">Submit form</button>
  63. </div>
  64. </form>

Supported elements

Validation styles are available for the following form controls and components:

  • <input>s and <textarea>s with .form-control (including up to one .form-control in input groups)
  • <select>s with .form-select
  • .form-checks

Validation - 图4

html

  1. <form class="was-validated">
  2. <div class="mb-3">
  3. <label for="validationTextarea" class="form-label">Textarea</label>
  4. <textarea class="form-control" id="validationTextarea" placeholder="Required example textarea" required></textarea>
  5. <div class="invalid-feedback">
  6. Please enter a message in the textarea.
  7. </div>
  8. </div>
  9. <div class="form-check mb-3">
  10. <input type="checkbox" class="form-check-input" id="validationFormCheck1" required>
  11. <label class="form-check-label" for="validationFormCheck1">Check this checkbox</label>
  12. <div class="invalid-feedback">Example invalid feedback text</div>
  13. </div>
  14. <div class="form-check">
  15. <input type="radio" class="form-check-input" id="validationFormCheck2" name="radio-stacked" required>
  16. <label class="form-check-label" for="validationFormCheck2">Toggle this radio</label>
  17. </div>
  18. <div class="form-check mb-3">
  19. <input type="radio" class="form-check-input" id="validationFormCheck3" name="radio-stacked" required>
  20. <label class="form-check-label" for="validationFormCheck3">Or toggle this other radio</label>
  21. <div class="invalid-feedback">More example invalid feedback text</div>
  22. </div>
  23. <div class="mb-3">
  24. <select class="form-select" required aria-label="select example">
  25. <option value="">Open this select menu</option>
  26. <option value="1">One</option>
  27. <option value="2">Two</option>
  28. <option value="3">Three</option>
  29. </select>
  30. <div class="invalid-feedback">Example invalid select feedback</div>
  31. </div>
  32. <div class="mb-3">
  33. <input type="file" class="form-control" aria-label="file example" required>
  34. <div class="invalid-feedback">Example invalid form file feedback</div>
  35. </div>
  36. <div class="mb-3">
  37. <button class="btn btn-primary" type="submit" disabled>Submit form</button>
  38. </div>
  39. </form>

Tooltips

If your form layout allows it, you can swap the .{valid|invalid}-feedback classes for .{valid|invalid}-tooltip classes to display validation feedback in a styled tooltip. Be sure to have a parent with position: relative on it for tooltip positioning. In the example below, our column classes have this already, but your project may require an alternative setup.

Validation - 图5

html

  1. <form class="row g-3 needs-validation" novalidate>
  2. <div class="col-md-4 position-relative">
  3. <label for="validationTooltip01" class="form-label">First name</label>
  4. <input type="text" class="form-control" id="validationTooltip01" value="Mark" required>
  5. <div class="valid-tooltip">
  6. Looks good!
  7. </div>
  8. </div>
  9. <div class="col-md-4 position-relative">
  10. <label for="validationTooltip02" class="form-label">Last name</label>
  11. <input type="text" class="form-control" id="validationTooltip02" value="Otto" required>
  12. <div class="valid-tooltip">
  13. Looks good!
  14. </div>
  15. </div>
  16. <div class="col-md-4 position-relative">
  17. <label for="validationTooltipUsername" class="form-label">Username</label>
  18. <div class="input-group has-validation">
  19. <span class="input-group-text" id="validationTooltipUsernamePrepend">@</span>
  20. <input type="text" class="form-control" id="validationTooltipUsername" aria-describedby="validationTooltipUsernamePrepend" required>
  21. <div class="invalid-tooltip">
  22. Please choose a unique and valid username.
  23. </div>
  24. </div>
  25. </div>
  26. <div class="col-md-6 position-relative">
  27. <label for="validationTooltip03" class="form-label">City</label>
  28. <input type="text" class="form-control" id="validationTooltip03" required>
  29. <div class="invalid-tooltip">
  30. Please provide a valid city.
  31. </div>
  32. </div>
  33. <div class="col-md-3 position-relative">
  34. <label for="validationTooltip04" class="form-label">State</label>
  35. <select class="form-select" id="validationTooltip04" required>
  36. <option selected disabled value="">Choose...</option>
  37. <option>...</option>
  38. </select>
  39. <div class="invalid-tooltip">
  40. Please select a valid state.
  41. </div>
  42. </div>
  43. <div class="col-md-3 position-relative">
  44. <label for="validationTooltip05" class="form-label">Zip</label>
  45. <input type="text" class="form-control" id="validationTooltip05" required>
  46. <div class="invalid-tooltip">
  47. Please provide a valid zip.
  48. </div>
  49. </div>
  50. <div class="col-12">
  51. <button class="btn btn-primary" type="submit">Submit form</button>
  52. </div>
  53. </form>

CSS

Variables

Added in v5.3.0

As part of Bootstrap’s evolving CSS variables approach, forms now use local CSS variables for validation for enhanced real-time customization. Values for the CSS variables are set via Sass, so Sass customization is still supported, too.

scss/_root.scss

  1. --#{$prefix}form-valid-color: #{$form-valid-color};
  2. --#{$prefix}form-valid-border-color: #{$form-valid-border-color};
  3. --#{$prefix}form-invalid-color: #{$form-invalid-color};
  4. --#{$prefix}form-invalid-border-color: #{$form-invalid-border-color};

These variables are also color mode adaptive, meaning they change color while in dark mode.

Sass variables

scss/_variables.scss

  1. $form-feedback-margin-top: $form-text-margin-top;
  2. $form-feedback-font-size: $form-text-font-size;
  3. $form-feedback-font-style: $form-text-font-style;
  4. $form-feedback-valid-color: $success;
  5. $form-feedback-invalid-color: $danger;
  6. $form-feedback-icon-valid-color: $form-feedback-valid-color;
  7. $form-feedback-icon-valid: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'><path fill='#{$form-feedback-icon-valid-color}' d='M2.3 6.73.6 4.53c-.4-1.04.46-1.4 1.1-.8l1.1 1.4 3.4-3.8c.6-.63 1.6-.27 1.2.7l-4 4.6c-.43.5-.8.4-1.1.1z'/></svg>");
  8. $form-feedback-icon-invalid-color: $form-feedback-invalid-color;
  9. $form-feedback-icon-invalid: url("data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' width='12' height='12' fill='none' stroke='#{$form-feedback-icon-invalid-color}'><circle cx='6' cy='6' r='4.5'/><path stroke-linejoin='round' d='M5.8 3.6h.4L6 6.5z'/><circle cx='6' cy='8.2' r='.6' fill='#{$form-feedback-icon-invalid-color}' stroke='none'/></svg>");

scss/_variables.scss

  1. $form-valid-color: $form-feedback-valid-color;
  2. $form-valid-border-color: $form-feedback-valid-color;
  3. $form-invalid-color: $form-feedback-invalid-color;
  4. $form-invalid-border-color: $form-feedback-invalid-color;

scss/_variables-dark.scss

  1. $form-valid-color-dark: $green-300;
  2. $form-valid-border-color-dark: $green-300;
  3. $form-invalid-color-dark: $red-300;
  4. $form-invalid-border-color-dark: $red-300;

Sass mixins

Two mixins are combined, through our loop, to generate our form validation feedback styles.

scss/mixins/_forms.scss

  1. @mixin form-validation-state-selector($state) {
  2. @if ($state == "valid" or $state == "invalid") {
  3. .was-validated #{if(&, "&", "")}:#{$state},
  4. #{if(&, "&", "")}.is-#{$state} {
  5. @content;
  6. }
  7. } @else {
  8. #{if(&, "&", "")}.is-#{$state} {
  9. @content;
  10. }
  11. }
  12. }
  13. @mixin form-validation-state(
  14. $state,
  15. $color,
  16. $icon,
  17. $tooltip-color: color-contrast($color),
  18. $tooltip-bg-color: rgba($color, $form-feedback-tooltip-opacity),
  19. $focus-box-shadow: 0 0 $input-btn-focus-blur $input-focus-width rgba($color, $input-btn-focus-color-opacity),
  20. $border-color: $color
  21. ) {
  22. .#{$state}-feedback {
  23. display: none;
  24. width: 100%;
  25. margin-top: $form-feedback-margin-top;
  26. @include font-size($form-feedback-font-size);
  27. font-style: $form-feedback-font-style;
  28. color: $color;
  29. }
  30. .#{$state}-tooltip {
  31. position: absolute;
  32. top: 100%;
  33. z-index: 5;
  34. display: none;
  35. max-width: 100%; // Contain to parent when possible
  36. padding: $form-feedback-tooltip-padding-y $form-feedback-tooltip-padding-x;
  37. margin-top: .1rem;
  38. @include font-size($form-feedback-tooltip-font-size);
  39. line-height: $form-feedback-tooltip-line-height;
  40. color: $tooltip-color;
  41. background-color: $tooltip-bg-color;
  42. @include border-radius($form-feedback-tooltip-border-radius);
  43. }
  44. @include form-validation-state-selector($state) {
  45. ~ .#{$state}-feedback,
  46. ~ .#{$state}-tooltip {
  47. display: block;
  48. }
  49. }
  50. .form-control {
  51. @include form-validation-state-selector($state) {
  52. border-color: $border-color;
  53. @if $enable-validation-icons {
  54. padding-right: $input-height-inner;
  55. background-image: escape-svg($icon);
  56. background-repeat: no-repeat;
  57. background-position: right $input-height-inner-quarter center;
  58. background-size: $input-height-inner-half $input-height-inner-half;
  59. }
  60. &:focus {
  61. border-color: $border-color;
  62. @if $enable-shadows {
  63. @include box-shadow($input-box-shadow, $focus-box-shadow);
  64. } @else {
  65. // Avoid using mixin so we can pass custom focus shadow properly
  66. box-shadow: $focus-box-shadow;
  67. }
  68. }
  69. }
  70. }
  71. // stylelint-disable-next-line selector-no-qualifying-type
  72. textarea.form-control {
  73. @include form-validation-state-selector($state) {
  74. @if $enable-validation-icons {
  75. padding-right: $input-height-inner;
  76. background-position: top $input-height-inner-quarter right $input-height-inner-quarter;
  77. }
  78. }
  79. }
  80. .form-select {
  81. @include form-validation-state-selector($state) {
  82. border-color: $border-color;
  83. @if $enable-validation-icons {
  84. &:not([multiple]):not([size]),
  85. &:not([multiple])[size="1"] {
  86. --#{$prefix}form-select-bg-icon: #{escape-svg($icon)};
  87. padding-right: $form-select-feedback-icon-padding-end;
  88. background-position: $form-select-bg-position, $form-select-feedback-icon-position;
  89. background-size: $form-select-bg-size, $form-select-feedback-icon-size;
  90. }
  91. }
  92. &:focus {
  93. border-color: $border-color;
  94. @if $enable-shadows {
  95. @include box-shadow($form-select-box-shadow, $focus-box-shadow);
  96. } @else {
  97. // Avoid using mixin so we can pass custom focus shadow properly
  98. box-shadow: $focus-box-shadow;
  99. }
  100. }
  101. }
  102. }
  103. .form-control-color {
  104. @include form-validation-state-selector($state) {
  105. @if $enable-validation-icons {
  106. width: add($form-color-width, $input-height-inner);
  107. }
  108. }
  109. }
  110. .form-check-input {
  111. @include form-validation-state-selector($state) {
  112. border-color: $border-color;
  113. &:checked {
  114. background-color: $color;
  115. }
  116. &:focus {
  117. box-shadow: $focus-box-shadow;
  118. }
  119. ~ .form-check-label {
  120. color: $color;
  121. }
  122. }
  123. }
  124. .form-check-inline .form-check-input {
  125. ~ .#{$state}-feedback {
  126. margin-left: .5em;
  127. }
  128. }
  129. .input-group {
  130. > .form-control:not(:focus),
  131. > .form-select:not(:focus),
  132. > .form-floating:not(:focus-within) {
  133. @include form-validation-state-selector($state) {
  134. @if $state == "valid" {
  135. z-index: 3;
  136. } @else if $state == "invalid" {
  137. z-index: 4;
  138. }
  139. }
  140. }
  141. }
  142. }

Sass maps

This is the validation Sass map from _variables.scss. Override or extend this to generate different or additional states.

scss/_variables.scss

  1. $form-validation-states: (
  2. "valid": (
  3. "color": var(--#{$prefix}form-valid-color),
  4. "icon": $form-feedback-icon-valid,
  5. "tooltip-color": #fff,
  6. "tooltip-bg-color": var(--#{$prefix}success),
  7. "focus-box-shadow": 0 0 $input-btn-focus-blur $input-focus-width rgba(var(--#{$prefix}success-rgb), $input-btn-focus-color-opacity),
  8. "border-color": var(--#{$prefix}form-valid-border-color),
  9. ),
  10. "invalid": (
  11. "color": var(--#{$prefix}form-invalid-color),
  12. "icon": $form-feedback-icon-invalid,
  13. "tooltip-color": #fff,
  14. "tooltip-bg-color": var(--#{$prefix}danger),
  15. "focus-box-shadow": 0 0 $input-btn-focus-blur $input-focus-width rgba(var(--#{$prefix}danger-rgb), $input-btn-focus-color-opacity),
  16. "border-color": var(--#{$prefix}form-invalid-border-color),
  17. )
  18. );

Maps of $form-validation-states can contain three optional parameters to override tooltips and focus styles.

Sass loops

Used to iterate over $form-validation-states map values to generate our validation styles. Any modifications to the above Sass map will be reflected in your compiled CSS via this loop.

scss/forms/_validation.scss

  1. @each $state, $data in $form-validation-states {
  2. @include form-validation-state($state, $data...);
  3. }

Customizing

Validation states can be customized via Sass with the $form-validation-states map. Located in our _variables.scss file, this Sass map is how we generate the default valid/invalid validation states. Included is a nested map for customizing each state’s color, icon, tooltip color, and focus shadow. While no other states are supported by browsers, those using custom styles can easily add more complex form feedback.