Validation List Web Component Demo

A web component comprising a list of validation rules for a field. See the README for installation and API documentation.

Matched rules show a check; unmatched rules show a cross. Both inherit the rule’s text color by default. Once a field has a value, localized hidden text also identifies each rule’s state. Without JavaScript, the fields and requirements remain visible, but these extra rules are not enforced. Always validate submitted values on the server.

Basic Password Validation

A password field with multiple validation requirements.

Username Validation

This example validates a username with capital letters, lowercase letters, and numbers.

<form>
  <label for="username">Username:</label>
  <input type="text" id="username" name="username" required>

  <form-validation-list for="username">
    <ul>
      <li data-pattern="[A-Z]+">At least one capital letter</li>
      <li data-pattern="[a-z]+">At least one lowercase letter</li>
      <li data-pattern="[\d]+">At least one number</li>
    </ul>
  </form-validation-list>

  <button type="submit">Submit</button>
</form>
  • At least one capital letter
  • At least one lowercase letter
  • At least one number

Strong Password Requirements

A more complex validation with length and special character requirements.

Password Strength Validation

Demonstrates multiple password requirements including length and special characters.

<form>
  <label for="password">Password:</label>
  <input type="password" id="password" name="password" required>

  <form-validation-list for="password">
    <ul>
      <li data-pattern=".{8,}">At least 8 characters</li>
      <li data-pattern="[A-Z]+">At least one uppercase letter</li>
      <li data-pattern="[a-z]+">At least one lowercase letter</li>
      <li data-pattern="[\d]+">At least one number</li>
      <li data-pattern="[!@#$%^&*]+">At least one special character (!@#$%^&*)</li>
    </ul>
  </form-validation-list>

  <button type="submit">Submit</button>
</form>
  • At least 8 characters
  • At least one uppercase letter
  • At least one lowercase letter
  • At least one number
  • At least one special character (!@#$%^&*)

Custom Styling with CSS Custom Properties

Customize the appearance using CSS custom properties.

Custom Icons and Colors

This example uses monochrome glyphs and green/red icon colors on an explicit white surface. Both colors have at least 4.5:1 contrast against that surface. The check and cross still distinguish states without color. Automatic forced-color adjustment remains enabled.

<style>
  .custom-styled {
    display: block;
    --rule-matched-icon: "✔";
    --rule-unmatched-icon: "✘";
    --rule-icon-size: 1.2em;
    --rule-matched-color: #17652e;
    --rule-unmatched-color: #982b39;
    background: #fff;
    color: #202020;
  }
</style>

<form>
  <label for="email">Email:</label>
  <input type="text" id="email" name="email" required>

  <form-validation-list for="email" class="custom-styled">
    <ul>
      <li data-pattern=".+@.+">Contains @ symbol</li>
      <li data-pattern=".+@.+\..+">Valid email format</li>
      <li data-pattern=".{6,}">At least 6 characters</li>
    </ul>
  </form-validation-list>

  <button type="submit">Submit</button>
</form>
  • Contains @ symbol
  • Valid email format
  • At least 6 characters

Customization Options

The component provides several attributes to customize its behavior and styling.

Custom Trigger Event and Delay

Use trigger-event="blur" to validate only when the field loses focus, and each-delay="0" to remove the cascade animation.

<form-validation-list
  for="custom-trigger"
  trigger-event="blur"
  each-delay="0">
  <ul>
    <li data-pattern="[A-Z]+">Capital letter</li>
    <li data-pattern="[a-z]+">Lowercase letter</li>
    <li data-pattern="[\d]+">Number</li>
  </ul>
</form-validation-list>
  • Capital letter
  • Lowercase letter
  • Number
Validation runs on blur, no cascade animation

Custom CSS Classes

Customize the class names applied to fields and rules for easier integration with CSS frameworks.

<style>
  input.is-valid { border: 2px solid FieldText; }
  input.is-invalid { border: 2px dashed FieldText; }
  .rule-pass { font-weight: bold; }
  .rule-fail { font-style: italic; }
</style>

<form-validation-list
  for="custom-classes"
  field-valid-class="is-valid"
  field-invalid-class="is-invalid"
  rule-matched-class="rule-pass"
  rule-unmatched-class="rule-fail">
  <ul>
    <li data-pattern=".{5,}">At least 5 characters</li>
    <li data-pattern="[!@#]+">Special char (!@#)</li>
  </ul>
</form-validation-list>
  • At least 5 characters
  • Special char (!@#)
Uses custom classes: is-valid/is-invalid, rule-pass/rule-fail. Solid/dashed field borders and check/cross icons distinguish states without color.

JavaScript API

Listening to validation events and using the programmatic API.

Event-Driven Validation

This example shows how to listen to validation events.

<script>
  const validationList = document.querySelector('#api-validation');

  validationList.addEventListener('form-validation-list:validated', (e) => {
    const { isValid, matchedRules, totalRules } = e.detail;
    console.log(`Matched ${matchedRules} of ${totalRules} rules`);
  });

  // Programmatically trigger validation
  const isValid = validationList.validate();
  console.log('Is valid:', isValid);

  // Check current state
  console.log('Current state:', validationList.isValid);
</script>
  • Only letters
  • At least 3 characters

Internationalization (i18n)

This example localizes the browser validation message, the live summary announcement, and the per-rule hidden state text.

Spanish Validation Messages

Using validation-message, announcement, and the rule-*-alt attributes keep the spoken experience localized.

<form>
  <label for="contrasena">Contraseña:</label>
  <input
    type="password"
    id="contrasena"
    name="contrasena"
    required>

  <form-validation-list
    for="contrasena"
    announcement="{matched} de {total} criterios cumplidos"
    rule-matched-alt="Criterio cumplido"
    rule-unmatched-alt="Criterio pendiente"
    validation-message="Por favor, cumple todos los requisitos ({matched} de {total})">
    <ul>
      <li data-pattern="[A-Z]+">Al menos una letra mayúscula</li>
      <li data-pattern="[a-z]+">Al menos una letra minúscula</li>
      <li data-pattern="[\d]+">Al menos un número</li>
      <li data-pattern="[!@#$%^&*]+">Al menos un carácter especial (!@#$%^&*)</li>
      <li data-pattern=".{8,}">Al menos 8 caracteres</li>
    </ul>
  </form-validation-list>

  <button type="submit">Enviar</button>
</form>
  • Al menos una letra mayúscula
  • Al menos una letra minúscula
  • Al menos un número
  • Al menos un carácter especial (!@#$%^&*)
  • Al menos 8 caracteres