Sub topic 13
Keeping user input and helping people recover
This chapter assumes you can write basic HTML forms and read a little JavaScript. You should also know how errors are shown and announced from the earlier chapters. Here the focus is on what happens after an error. The user has already spent effort on the form, and losing that effort is the fastest way to lose the user.
Never make people retype what was right
When validation fails, the page must come back with every value the user entered still in place. Only the invalid fields should carry an error. This matters most on server-rendered forms. A full page reload resets every control unless the server writes the submitted values back into the HTML.
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email"
value="sam@example" aria-invalid="true" aria-describedby="email-error">
<p id="email-error">Error: Enter an email address in the format [email protected]</p>The value attribute above holds what the user typed, so they can fix the missing ending instead of starting over. Escape that value on the server before you put it in the page. Echoing raw input into HTML is a cross-site scripting hole.
Some fields should not be restored:
- Passwords: leave them empty and say so near the field, because echoing them back puts a secret in the page source.
- Payment card numbers and security codes: ask for them again.
- File uploads: browsers do not allow a script or server to refill them. Tell the user their file needs to be chosen again, or keep the upload on the server and show its name.
Keep a draft on long forms
Long forms are exposed to accidental navigation, expired sessions, and the Back button. A draft saved in the browser lets people return to where they stopped. This example uses sessionStorage, which lasts for the tab's lifetime. It handles text fields, selects, and radio groups. Checkboxes need extra handling that is not shown here.
const form = document.querySelector('form');
const KEY = 'signup-draft';
const draft = JSON.parse(sessionStorage.getItem(KEY) || '{}');
// Restore saved values when the page loads.
for (const [name, value] of Object.entries(draft)) {
if (form.elements[name]) form.elements[name].value = value;
}
// Save each change, skipping passwords.
form.addEventListener('input', (event) => {
const field = event.target;
if (!field.name || field.type === 'password') return;
draft[field.name] = field.value;
sessionStorage.setItem(KEY, JSON.stringify(draft));
});
// Clear the draft once the form is sent.
form.addEventListener('submit', () => sessionStorage.removeItem(KEY));Clearing on submit means that a server-side rejection must still return the values, as in the first example. The two techniques cover different failures, so use both.
Design the recovery path
Several habits support this flow:
- Keep the error visible after the fix attempt. Remove an inline error only when the field becomes valid, not when the user starts typing.
- Link the summary to the fields. Each summary item should be a link that moves focus to its field.
- Avoid a Reset button. It sits next to Submit, one slip erases everything, and almost nobody wants it. If you need one, ask for confirmation first.
- Warn before leaving. For long forms with unsaved changes, a
beforeunloadprompt can prevent accidental loss. Use it only while the form has unsaved edits, or it becomes noise. - Handle timeouts openly. If a session expires, tell the user before it happens. Keep their entries so they can sign in again and continue.
- Let people review before sending. For high-stakes forms, add a summary step with an Edit link for each section, or a way to undo.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Reloading with an empty form | Users abandon the form | Write submitted values back into the HTML |
| Echoing values without escaping | Cross-site scripting risk | Escape on output |
| Restoring passwords | Secret appears in page source | Leave blank and explain |
| Clearing a field because it is invalid | Users cannot see what was wrong | Keep the value and explain the problem |
Exercise
A server-rendered checkout form rejects a submission because the postcode is invalid. Which values should the re-rendered page contain, and which should be left empty?
Show answer
Keep every value the user entered, including the invalid postcode, so they can correct it. Leave the card security code and any password empty and ask for them again. Escape all echoed values. A file upload also cannot be refilled, so ask the user to choose the file again or show the name of the file stored on the server.