Skip to the main content

Interactive Form Validation

FreeUses XON Forms

A contact form with per-field rules, messages tied to each field and an error summary that links to the first problem, using XON.

Checked on 3 December 2026 in Chrome 143.0.7499.169: 12 automated checks. What the checks are

Preview and code

HTML

<section class="ifv" aria-labelledby="ifv-title">
  <h2 id="ifv-title" class="ifv__title">Send a message</h2>
  <p class="ifv__lead">Example form. Fields are checked when you leave them and again when you send.</p>

  <form class="ifv__form" id="ifv-form" data-errors-in="ifv-summary" data-errors-title="Please fix these before sending" data-on-submit="ifvSend" data-if="!ifv.sent">
    <div id="ifv-summary" class="ifv__summary" hidden></div>

    <div class="ifv__field">
      <label class="ifv__label" for="ifv-name">Your name</label>
      <input class="ifv__input" id="ifv-name" name="name" type="text" autocomplete="name" required minlength="2" data-model="ifv.name" data-message-required="Enter your name" data-message-minlength="Use at least 2 characters" data-error-in="ifv-name-error">
      <p class="ifv__error" id="ifv-name-error"></p>
    </div>

    <div class="ifv__field">
      <label class="ifv__label" for="ifv-email">E-mail address</label>
      <input class="ifv__input" id="ifv-email" name="email" type="email" autocomplete="email" required data-message-required="Enter your e-mail address" data-message-type="Write it like name@example.com" data-error-in="ifv-email-error">
      <p class="ifv__error" id="ifv-email-error"></p>
    </div>

    <div class="ifv__field">
      <label class="ifv__label" for="ifv-message">Message <span class="ifv__hint">at least three words</span></label>
      <textarea class="ifv__input ifv__input--area" id="ifv-message" name="message" rows="4" required data-rule="min-words:3" data-message-required="Write a short message" data-message-min-words="Write at least three words" data-error-in="ifv-message-error"></textarea>
      <p class="ifv__error" id="ifv-message-error"></p>
    </div>

    <div class="ifv__field ifv__field--check">
      <input class="ifv__check" id="ifv-consent" name="consent" type="checkbox" required data-message-required="Tick the box to continue" data-error-in="ifv-consent-error">
      <label class="ifv__check-label" for="ifv-consent">I understand this is an example and nothing is stored</label>
      <p class="ifv__error" id="ifv-consent-error"></p>
    </div>

    <button type="submit" class="ifv__submit" id="ifv-submit">Send message</button>
  </form>

  <div class="ifv__done" id="ifv-done" role="status" tabindex="-1" data-if="ifv.sent">
    <svg class="ifv__tick" viewBox="0 0 24 24" width="40" height="40" aria-hidden="true" focusable="false"><circle cx="12" cy="12" r="11" fill="currentColor"/><path d="M7 12.5l3.2 3.2L17 9" fill="none" stroke="var(--ifv-on-ok)" stroke-width="2.4" stroke-linecap="round" stroke-linejoin="round"/></svg>
    <p class="ifv__done-title">Thank you, <span data-text="ifv.name"></span>.</p>
    <p class="ifv__done-text">This is an example form, so nothing was sent.</p>
    <button type="button" class="ifv__again" id="ifv-again" data-on-click="ifvAgain">Start again</button>
  </div>
</section>

CSS

.ifv {
  --ifv-surface: #ffffff;
  --ifv-fg: #1b2032;
  --ifv-muted: #4a5266;
  --ifv-line: #8a92a8;
  --ifv-soft-line: #d3d8e6;
  --ifv-accent: #4338ca;
  --ifv-on-accent: #ffffff;
  --ifv-error: #b42318;
  --ifv-error-bg: #fef3f2;
  --ifv-ok: #0f6b3f;
  --ifv-on-ok: #ffffff;
  --ifv-ring: #4338ca;
  box-sizing: border-box;
  width: min(100%, 30rem);
  padding: 1.5rem;
  color: var(--ifv-fg);
  background: var(--ifv-surface);
  border: 1px solid var(--ifv-soft-line);
  border-radius: 16px;
  box-shadow: 0 10px 30px -18px rgba(27, 32, 50, 0.4);
}
.ifv *, .ifv *::before, .ifv *::after { box-sizing: border-box; }
.ifv__title { margin: 0; font-size: 1.375rem; line-height: 1.25; letter-spacing: -0.01em; }
.ifv__lead { margin: 0.35rem 0 1.25rem; color: var(--ifv-muted); font-size: 0.9375rem; line-height: 1.5; }
.ifv__form { display: grid; gap: 1rem; }
.ifv__field { display: grid; gap: 0.35rem; }
.ifv__label { font-size: 0.9375rem; font-weight: 600; }
.ifv__hint { font-weight: 400; color: var(--ifv-muted); font-size: 0.8125rem; margin-left: 0.35rem; }
.ifv__input {
  width: 100%;
  min-height: 44px;
  padding: 0.55rem 0.8rem;
  font: inherit;
  color: var(--ifv-fg);
  background: var(--ifv-surface);
  border: 1.5px solid var(--ifv-line);
  border-radius: 10px;
}
.ifv__input--area { min-height: 6.5rem; resize: vertical; line-height: 1.5; }
.ifv__input:focus-visible, .ifv__check:focus-visible, .ifv__submit:focus-visible, .ifv__again:focus-visible, .ifv__summary:focus-visible { outline: 3px solid var(--ifv-ring); outline-offset: 2px; }
.ifv__input[aria-invalid="true"] { border-color: var(--ifv-error); background: var(--ifv-error-bg); }
.ifv__error { margin: 0; color: var(--ifv-error); font-size: 0.875rem; font-weight: 500; line-height: 1.4; }
.ifv__error:empty { display: none; }
.ifv__error:not(:empty)::before { content: "\26A0\FE0E\00A0"; content: "\26A0\FE0E\00A0" / ""; }
.ifv__field--check { grid-template-columns: auto 1fr; align-items: start; column-gap: 0.7rem; }
.ifv__check { width: 22px; height: 22px; margin: 0.1rem 0 0; accent-color: var(--ifv-accent); cursor: pointer; }
.ifv__check-label { font-size: 0.9375rem; line-height: 1.45; cursor: pointer; }
.ifv__field--check .ifv__error { grid-column: 2; }
.ifv__submit, .ifv__again {
  min-height: 48px;
  padding: 0.6rem 1.2rem;
  font: inherit;
  font-weight: 600;
  color: var(--ifv-on-accent);
  background: var(--ifv-accent);
  border: 2px solid var(--ifv-accent);
  border-radius: 10px;
  cursor: pointer;
}
.ifv__submit:hover, .ifv__again:hover { filter: brightness(1.1); }
.ifv__again { color: var(--ifv-accent); background: transparent; }
.ifv__again:hover { background: rgba(67, 56, 202, 0.08); filter: none; }
.ifv__summary {
  padding: 0.9rem 1rem;
  background: var(--ifv-error-bg);
  border: 2px solid var(--ifv-error);
  border-radius: 12px;
}
.ifv__summary h2, .ifv__summary h3, .ifv__summary strong { margin: 0 0 0.4rem; color: var(--ifv-error); font-size: 1rem; line-height: 1.3; }
.ifv__summary ul { margin: 0; padding-left: 1.2rem; }
.ifv__summary li { margin: 0.2rem 0; }
.ifv__summary a { color: var(--ifv-error); font-weight: 600; text-underline-offset: 2px; }
.ifv__summary a:focus-visible { outline: 3px solid var(--ifv-ring); outline-offset: 2px; }
.ifv__done { display: grid; justify-items: center; gap: 0.3rem; padding: 1.25rem 0.5rem 0.5rem; text-align: center; border-radius: 12px; }
.ifv__done[hidden] { display: none; }
.ifv__done:focus-visible { outline: 3px solid var(--ifv-ring); outline-offset: 4px; }
.ifv__tick { color: var(--ifv-ok); }
.ifv__done-title { margin: 0.4rem 0 0; font-size: 1.25rem; font-weight: 700; }
.ifv__done-text { margin: 0 0 0.9rem; color: var(--ifv-muted); }
@media (max-width: 30em) { .ifv { padding: 1.15rem; } }
@media (prefers-color-scheme: dark) {
  .ifv {
    --ifv-surface: #1a1e2e;
    --ifv-fg: #edf0fa;
    --ifv-muted: #b9bfd3;
    --ifv-line: #7c84a0;
    --ifv-soft-line: #3a4157;
    --ifv-accent: #aeb1ff;
    --ifv-on-accent: #14162b;
    --ifv-error: #ffa59b;
    --ifv-error-bg: #3b1d24;
    --ifv-ok: #6ee7a8;
    --ifv-on-ok: #0b1f15;
    --ifv-ring: #c9cbff;
    box-shadow: none;
  }
  .ifv__again:hover { background: rgba(174, 177, 255, 0.14); }
}

JavaScript

// The rules, the messages and the error summary come from the XON validate part: they are written as
// data-rule, data-message-* and data-error-in on the fields. This script only handles a valid submit.
var ifv = xon.data('ifv', { sent: false, name: '' });

function ifvSend(event) {
  // The preview cannot submit, and an example sends nothing: cancel the browser's own submit.
  event.preventDefault();
  ifv.sent = true;
  xon.flush();
  document.getElementById('ifv-done').focus();
}

function ifvAgain() {
  document.getElementById('ifv-form').reset();
  ifv.name = '';
  ifv.sent = false;
  xon.flush();
  document.getElementById('ifv-name').focus();
}

xon.use(ifvSend, ifvAgain);

Accessibility notes

Written by the author of the snippet. Each note has a test step or a markup check that reads the page.

  • Every field has a visible label, and its message is tied to it with aria-describedby.
  • A failed submit moves focus to a summary whose links jump to each field.
  • The confirmation is a status message that receives focus.
Forms
Free

Text Inputs

Text, email, number, search and multi-line fields with a label, a hint and an error line tied to the field, switched on by the browser's own checks.

  • HTML
  • CSS
  • No library
  • Forms
  • Inputs