Focus the First Invalid Field
Write the code, see it run, pass the checks.
The problem
- hard
- Code
- Pro
A sign-up form has inputs labelled "Name", "Email" and "Password" (type="password"), in that order, and a "Create account" button; give it noValidate. On submit, check all three: Name must not be blank ("Enter your name"); Email, ignoring spaces at either end, needs an "@" with something before and after it ("Enter a valid email"); Password needs at least 8 characters ("Use at least 8 characters"). For each invalid field, set aria-invalid="true" and show its error next to it (outside the label). Then move keyboard focus to the first invalid field, and above the fields show a summary: a heading "There are 2 problems" (or "There is 1 problem") and a list labelled "Problems" (aria-label is fine) with a button per error, its text the error itself, in form order. Clicking one focuses its field without submitting. Errors are only checked on submit. A valid submit replaces the form with "Account created for Ada" (the trimmed name).
Your code must pass
- Submitting moves focus to the first invalid field
- Invalid fields are marked and explained
- A summary counts and lists the problems
- Clicking a problem focuses its field
- One problem is singular, and a valid form creates the account