Skip to main content

Help users recover from HTML form errors with a linked summary

Updated 4 min readMarkdown

A long form can seem to do nothing after you press Submit. The error may be above the visible area or several Tab stops away. Keep each message beside its field, add a short list of the errors near the top, and move focus to the list after a failed submission. Let each link take the user straight to the relevant control.

Start with native field semantics

Keep the label visible and associate it with the control using matching for and id values. The label gives the field its name. The hint describes what goes in it. You can keep the hint visible and reserve another element for an error.

<label for="email">Email address (required)</label>
<p id="email-hint">Use a sample address like learner@example.com.</p>
<input id="email" type="email" required aria-describedby="email-hint">
<p id="email-error" hidden></p>

The browser still exposes input.validity.valueMissing and input.validity.typeMismatch when your enhanced form suppresses the browser's own validation pop-ups.

Build the summary beside the form

Give the summary a heading and a list. tabindex="-1" means your script can focus the summary after a failed submission. The links inside still follow normal Tab order.

<section id="errors" tabindex="-1" aria-labelledby="errors-title" hidden>
  <h2 id="errors-title">Check the marked fields</h2>
  <ul></ul>
</section>
<p id="result" role="status"></p>

When a field fails validation, show its message beside the input and add the identical wording to the list. Add aria-invalid="true" and append the error's ID to the hint already in aria-describedby. A user who jumps straight to the input can then encounter both the instructions and the error. After correcting and resubmitting the field, remove the error ID and leave the hint associated.

Move focus once after submission

The following runnable practice page has name and email fields. The form suppresses the default submit only after its script sets up the handler. It displays feedback locally using demo values.

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Workshop signup practice</title>
<main>
  <h1>Workshop signup practice</h1>
  <p>Try the form with demo values. It sends no data.</p>
  <section id="errors" tabindex="-1" aria-labelledby="errors-title" hidden>
    <h2 id="errors-title">Check the marked fields</h2>
    <ul></ul>
  </section>
  <p id="result" role="status"></p>
  <noscript><p>Enable JavaScript to use this practice form.</p></noscript>
  <form>
    <div>
      <label for="name">Name (required)</label>
      <input id="name" autocomplete="name" required>
      <p class="error" id="name-error" hidden></p>
    </div>
    <div>
      <label for="email">Email address (required)</label>
      <p class="hint" id="email-hint">Use a sample address like learner@example.com.</p>
      <input id="email" type="email" autocomplete="email" aria-describedby="email-hint" required>
      <p class="error" id="email-error" hidden></p>
    </div>
    <p><button type="submit" disabled>Check the form</button></p>
  </form>
</main>
<script>
  const form = document.querySelector('form');
  const summary = document.querySelector('#errors');
  const errorLinks = summary.querySelector('ul');
  const result = document.querySelector('#result');
  const inputs = [document.querySelector('#name'), document.querySelector('#email')];
  const hints = new Map(inputs.map(input => [input, input.getAttribute('aria-describedby')]));

  function getError(input) {
    if (input.validity.valueMissing || !input.value.trim()) {
      return input.id === 'name' ? 'Enter a name.' : 'Enter an email address.';
    }
    return input.validity.typeMismatch ? 'Enter an address like learner@example.com.' : '';
  }

  function renderFieldError(input, message) {
    const error = document.querySelector('#' + input.id + '-error');
    error.textContent = message;
    error.hidden = !message;
    const description = [hints.get(input), message && error.id].filter(Boolean).join(' ');
    if (description) input.setAttribute('aria-describedby', description);
    else input.removeAttribute('aria-describedby');
    if (message) input.setAttribute('aria-invalid', 'true');
    else input.removeAttribute('aria-invalid');
  }

  form.addEventListener('submit', event => {
    event.preventDefault();
    result.textContent = '';
    errorLinks.replaceChildren();
    for (const input of inputs) {
      const message = getError(input);
      renderFieldError(input, message);
      if (!message) continue;

      const item = document.createElement('li');
      const link = document.createElement('a');
      link.href = '#' + input.id;
      link.textContent = message;
      link.addEventListener('click', event => {
        event.preventDefault();
        input.focus();
      });
      item.append(link);
      errorLinks.append(item);
    }

    summary.hidden = !errorLinks.children.length;
    if (summary.hidden) result.textContent = 'All fields passed the practice check.';
    else summary.focus();
  });
  form.noValidate = true;
  form.querySelector('button').disabled = false;
</script>
</html>

After a failed submission, check that focus reaches the summary. Tab to an error link and press Enter. Your cursor should appear in the matching field. Fill in a name, enter abc as the email, and resubmit. Only the email error should remain. Correct it to learner@example.com and resubmit. The summary should disappear, both fields should lose their invalid state, and the form should expose a success message. Try the same sequence at a narrow width and with your usual screen reader. A keyboard check verifies focus movement and can’t tell you exactly what a particular screen reader announces.

For a production form, handle validation on the server as well. Return specific messages, preserve safe field values, and make the error summary usable if JavaScript fails. Server-side validation also handles errors a browser cannot know about, such as whether a workshop is full.

If you need somewhere to experiment with the example, open the Edit HTML Online HTML editor and paste the single file into the HTML pane. If preview settings block JavaScript, save it as an HTML file and open it locally.

Further reading includes the W3C form notifications tutorial and the GOV.UK Design System’s error summary guidance.

Disclosure. The Edit HTML Online team prepared this tutorial with AI assistance.

About the author

  • Free browser-based web development tools and step-by-step examples for learning HTML, CSS and JavaScript. https://edithtmlonline.com/

No comments yet. Be the first to comment.