StealThis .dev

Form — Top error summary + jump-to-field

An accessible GOV.UK-style form pattern that, on an invalid submit, renders a focused error-summary box at the top listing every invalid field as a clickable link. Each link scrolls to and focuses its field, while inline messages, aria-invalid, and aria-describedby keep the field-level state in sync. Fixing a field removes it from the summary live, and a clean submit swaps in a success confirmation panel — all in dependency-free vanilla JavaScript.

Open in Lab
html css vanilla-js
Targets: JS HTML

Code

Top error summary + jump-to-field

A membership signup form that follows the GOV.UK error-summary pattern. When you submit with mistakes, an alert box appears at the top of the form listing every invalid field in document order. Each entry is a real link — activating it smooth-scrolls to the field and moves keyboard focus straight onto the control, so users never have to hunt for what went wrong. The summary itself receives focus on a failed submit, which means screen-reader users hear the full list of problems immediately.

Every field also carries its own inline state: a red border, a helper error message, aria-invalid="true", and an updated aria-describedby that points at both the hint and the error text. Validation is real and field-specific — full name, email format, a six-digit member ID, a minimum age of 18, a chosen plan, an 8-character password with a digit, and an accepted terms checkbox. After the first submit the form switches to live correction: fixing a field removes it from the summary, flips it to a green valid state, and refreshes the heading count without stealing focus.

A clean submit hides the form and reveals a personalised confirmation panel, while a lightweight toast helper surfaces transient feedback. The layout uses a two-column grid that stacks to a single column under 520px, with full focus, error, success, and disabled field states, and it respects prefers-reduced-motion.