View on GitHub

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>

Sass

Variables

  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>");

Mixins

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

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

Map

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

  1. $form-validation-states: (
  2. "valid": (
  3. "color": $form-feedback-valid-color,
  4. "icon": $form-feedback-icon-valid
  5. ),
  6. "invalid": (
  7. "color": $form-feedback-invalid-color,
  8. "icon": $form-feedback-icon-invalid
  9. )
  10. );

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

Loop

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.

  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.