Menu

Validation

Validation states mark a field as valid or invalid and show a message under it, so the user sees what to fix.

Validation states

Validation states mark a field as valid or invalid, so the user sees which fields to correct and, for an invalid one, why.

To apply the validation state to the form control, use the .is-valid and .is-invalid classes. For assistive tech, also set aria-invalid="true" on invalid controls.

<div>
  <label class="form-label" for="state-valid">Valid state</label>
  <input type="text" id="state-valid" class="form-control is-valid" aria-invalid="false" />
</div>
<div>
  <label class="form-label" for="state-invalid">Invalid state</label>
  <input type="text" id="state-invalid" class="form-control is-invalid" aria-invalid="true" />
</div>

Validation feedback

Add a message under the field with the .valid-feedback or .invalid-feedback class. Link the message with aria-describedby when you can.

Please provide a valid city.
<div>
  <label class="form-label required" for="city-invalid">City</label>
  <input type="text" class="form-control is-invalid" id="city-invalid" required aria-invalid="true" aria-describedby="city-invalid-feedback" />
  <div class="invalid-feedback" id="city-invalid-feedback">Please provide a valid city.</div>
</div>

Use .valid-feedback to confirm that a value is correct.

Looks good!
<div>
  <label class="form-label required" for="city-valid">City</label>
  <input type="text" class="form-control is-valid" id="city-valid" required value="Warsaw" aria-describedby="city-valid-feedback" />
  <div class="valid-feedback" id="city-valid-feedback">Looks good!</div>
</div>

Validate after interaction

To show states without any JavaScript, add data-bs-validate to a <form>. Fields are styled with the :user-invalid pseudo-class, so an error shows only after the user has interacted with the field and left it.

Add data-bs-validate="valid" to also show the success styling with :user-valid.

Please provide a valid email.
Looks good!
Please provide a city.
<form data-bs-validate="valid" novalidate>
  <div class="mb-3">
    <label class="form-label required" for="user-email">Email</label>
    <input type="email" id="user-email" class="form-control" required placeholder="[email protected]" />
    <div class="invalid-feedback">Please provide a valid email.</div>
    <div class="valid-feedback">Looks good!</div>
  </div>
  <div>
    <label class="form-label required" for="user-city">City</label>
    <input type="text" id="user-city" class="form-control" required />
    <div class="invalid-feedback">Please provide a city.</div>
  </div>
</form>

The .was-validated class on a form and the .is-valid / .is-invalid classes on a field work as before. Use .is-* for server-side validation, and data-bs-validate when the browser checks the field.

Subtle validation states

For a quieter version, show a tick or a cross icon instead of the colored frame and the feedback message.

To do this, use the .is-valid-lite and .is-invalid-lite classes.

These variants drop the colored frame and the feedback message, so the state is left to a small icon. Keep aria-invalid on the field, and keep the reason in text somewhere the field points to - otherwise the error exists only as a symbol.

<div>
  <label class="form-label" for="state-valid-lite">Valid state</label>
  <input type="text" id="state-valid-lite" class="form-control is-valid is-valid-lite" aria-invalid="false" />
</div>
<div>
  <label class="form-label" for="state-invalid-lite">Invalid state</label>
  <input type="text" id="state-invalid-lite" class="form-control is-invalid is-invalid-lite" aria-invalid="true" />
</div>

Accessibility

  • Colour alone must not say that a field is wrong. Keep aria-invalid="true" on the field and a message in text.
  • Give the feedback element an id and point the field at it with aria-describedby, so the message is read with the field rather than after the form.
  • Say what to do, not only what failed. "Please provide a valid city" beats "Invalid".
  • On submit, move focus to the first field that failed. Otherwise a keyboard user has to hunt for it.
  • Validate when the user leaves a field, not on every keystroke. A message that appears and disappears while typing is announced over and over.

CSS

Sass variables

Override these variables before importing Tabler to change the defaults.

Source: core/scss/_variables.scss.

$form-validation-states: (
  'valid': (
    'color': var(--form-valid-color),
    'icon': $form-feedback-icon-valid,
    'tooltip-color': #fff,
    'tooltip-bg-color': var(--success),
    'focus-ring-color': focus-ring-color(var(--success)),
    'border-color': var(--form-valid-border-color),
  ),
  'invalid': (
    'color': var(--form-invalid-color),
    'icon': $form-feedback-icon-invalid,
    'tooltip-color': #fff,
    'tooltip-bg-color': var(--danger),
    'focus-ring-color': focus-ring-color(var(--danger)),
    'border-color': var(--form-invalid-border-color),
  ),
);