Form with Validation Summary Template

Improve usability and guide users by presenting all form errors in a convenient summary list.

When a user submits an invalid form, this template prevents submission and instead generates a summary box at the top, listing all errors with clickable links to the corresponding fields.

Screenshot of a form with a red error summary box at the top, listing all validation failures.

Get Source Code → Preview →

About this Validation Summary Form

For long forms, users might not see an inline error that's off-screen. A validation summary solves this by gathering all errors in one place. This pattern is a major usability win and is recommended by the W3C.

The workflow, managed by JavaScript, is as follows:

  1. When the user attempts to submit the form, JavaScript prevents the default action.
  2. It loops through all required fields and checks their validity.
  3. For each invalid field, it adds its error message to an array and applies an error style to the field itself.
  4. If there are any errors, the script dynamically builds an unordered list from the array of errors and displays it in a dedicated "summary box" at the top of the form. Each list item is a link that, when clicked, jumps the user's focus directly to the invalid input. The summary box is given role="alert" so screen readers will announce it automatically.

Features

Dependencies

Uses pure HTML and CSS, with a self-contained JavaScript block for the validation logic.

Code

Here's the full code for the form with a validation summary:

View Output