Web component / Examples

<form-validation-list>

Check a field against a visible list of regular expression requirements. 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. Native required checks still apply. These illustrative patterns aren’t comprehensive password or email validation, and matching them doesn’t establish password strength.

Check a username while typing

The default input delay is 250 milliseconds after the last input event. The list supplies a description for the field alongside any existing descriptions; its polite live region summarizes the number of matching rules while typing.

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

Check password requirements

A more complex validation with length and special character requirements.

Match length and character rules

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 (!@#$%^&*)

Choose when to validate

Validate on leaving a field instead of after input events. The list stays associated with the field while typing.

Validate when the field loses focus

Use trigger-event="blur" to validate only when the field loses focus, and each-delay="0" to remove the cascade animation. Type Ab1, then press Tab to leave the field. The component updates custom validity immediately on blur. See the timing reference for typing summaries and description restoration.

<label for="custom-trigger">Try me (blur to validate):</label>
<input type="text" id="custom-trigger" name="custom-trigger" required>
<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

Check requirements with a button

Use validate() to check an assigned value before a blur event. This example reports rule counts from form-validation-list:validated, then checks browser validity, which includes the native required constraint and the component’s custom validity.

Report matching rules and browser validity

Enter abc for two matching rules or 1 for none, then activate “Check requirements.” An empty field also fails the native required check. The result doesn’t move focus.

<form id="form4">
<label for="api-field">Test Field:</label>
<input type="text" id="api-field" name="api-field" required>
<form-validation-list for="api-field" id="api-validation"
  trigger-event="blur" each-delay="0">
  <ul>
    <li data-pattern="^[a-zA-Z]+$">Only letters</li>
    <li data-pattern=".{3,}">At least 3 characters</li>
  </ul>
</form-validation-list>
<button type="button" id="check-validity">Check requirements</button>
<div id="message4" role="status"></div>
</form>

<script type="module">
  import '../define.js';
  const validationList = document.getElementById('api-validation');
  const field = document.getElementById('api-field');
  const result = document.getElementById('message4');
  validationList.addEventListener('form-validation-list:validated', (event) => {
    const { matchedRules, totalRules } = event.detail;
    result.textContent = `${matchedRules} of ${totalRules} requirements matched. Browser validity: ${field.checkValidity() ? 'valid' : 'invalid'}.`;
  });
  document.getElementById('check-validity').addEventListener('click', () => {
    validationList.validate();
  });
  // Keep this demonstration on the page, including implicit submission.
  document.getElementById('form4').addEventListener('submit', (event) => {
    event.preventDefault();
  });
</script>
  • Only letters
  • At least 3 characters

Use your application’s vocabulary or another language

Set individual message attributes to use words such as “requirements” instead of “criteria.” The same attributes translate the custom validation message, typing summary, and hidden rule states. There’s no shared translation object; set all four attributes on each instance.

Spanish Validation Messages

This form uses Spanish labels, rules, and messages. lang="es" identifies its language, including the generated text. See the message reference.

<form lang="es">
  <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

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=".+@.+\..+">Contains @ followed by a dot</li>
      <li data-pattern=".{6,}">At least 6 characters</li>
    </ul>
  </form-validation-list>

  <button type="submit">Submit</button>
</form>
  • Contains @ symbol
  • Contains @ followed by a dot
  • At least 6 characters

Style fields and rules with your own classes

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>

<label for="custom-classes">Custom class names:</label>
<input type="text" id="custom-classes" name="custom-classes" required>
<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.