A sign-up form in some app asks for 12 things at once: name, date of birth, full address, postcode, mother's maiden name, and so on. By the ninth field you start wondering what all this is for. Many people stop right there.
A form that's easy to fill in works like a conversation: the page asks, the visitor answers. Picture the receptionist at a homestay writing down a guest's details. A good receptionist asks one clear question at a time and doesn't ask anything unnecessary. They wait for the guest to finish speaking. If the number the guest gives is one digit short, they say which part is short and how to fix it. They don't interrupt in the middle of a sentence with "wrong!". In a form, each question is a field, and the receptionist's correction is an error message.
Ask only for what's needed
The dates and number of nights are already chosen on the room card. To book, Mrs. Sri needs only three things:
| Field | Why it's needed | Required? |
|---|---|---|
| Full name | so Mrs. Sri knows who is coming | yes |
| WhatsApp number | the confirmation and directions are sent over WhatsApp | yes |
| a booking receipt, if the guest wants one | optional, labelled "(optional)" |
Every extra field is one more question that can make someone stop. If a form has to be long, for example a sign-up form with 12 fields, check each field first: is it needed now, or can it be asked later? Then split the form into steps, and put only what's truly required in the first step.
Labels on top, one column
A label is the name of a field, written next to it, such as "WhatsApp number".
- The label above its field. On a phone there's no room beside the field. Matteo Penzo's eye-tracking study (2006, with a device that records where the eyes look) measured how long the eye takes to move from a label to its field. From a label above the field, it took about 50 ms (milliseconds). From a left-aligned label beside the field, about 500 ms.
- One column, top to bottom. Put the fields one below the other, in a single column. The eye moves straight down. Two columns side by side make the eye zigzag, and a field in the right-hand column is easy to miss.
- A placeholder isn't a label. A placeholder is the example text inside an empty field. It disappears as soon as the guest starts typing, and it's usually a light grey like
#9CA3AF(2.54:1, a contrast fail). Use a placeholder only for an example, or write the example as help text under the field.
A label is connected to its field by a for attribute that exactly matches the field's id (the field's unique name). This connection makes two things work. A click on the label moves the focus (where typing goes) to the field. And a screen reader (a program that reads the page aloud for blind users) says "WhatsApp number" when the field gets focus.
In the exercise, the form has inputs with id="name" and id="email" and no labels. Add <label for="name">Name</label> and <label for="email">Email</label>, change the email input to type="email", and give both inputs required.
Input types and autocomplete
This is Mrs. Sri's booking form. Each field is an <input> tag with its <label> above it:
<form class="booking" novalidate>
<label for="name">Full name</label>
<input id="name" name="name" type="text" autocomplete="name" required>
<label for="wa">WhatsApp number</label>
<input id="wa" name="wa" type="tel" autocomplete="tel" required aria-describedby="wa-help wa-error">
<p id="wa-help" class="help">Example: 07700 900123 or +44 7700 900123</p>
<p id="wa-error" class="error" hidden></p>
<label for="email">Email (optional)</label>
<input id="email" name="email" type="email" autocomplete="email">
<button type="submit">Book the room, pay on arrival</button>
</form>
type="tel"brings up the phone keypad on a phone.type="email"brings up a keyboard with an @ key, and the browser also checks whether there's an @.autocomplete="name","tel"and"email"tell the browser what the field holds. So data saved on the guest's phone can be filled in with one tap. For fields about the user, WCAG 1.3.5 (Identify Input Purpose) asks for this.requiredmarks a field as required.novalidateon the<form>turns off the browser's own messages, because we write ours.aria-describedbyconnects the example (wa-help) and the error message (wa-error) to the field, so a screen reader reads them too. Thehiddenattribute hides the error message until something is wrong.- Don't use
type="number"for a phone number. In our test, Chromium and WebKit silently removed the spaces and the + sign while typing, and Firefox emptied the value. As a number, the leading 0 is lost too:7700900123. type="tel"checks nothing by itself: even "abc" counts as valid. Checking the format is our job.
Validation: when it shows, what it says
Validation means checking what is in a field, then showing an error message if it's wrong. Mr. Hadi types his WhatsApp number into Mrs. Sri's booking form: 07700-900123. The moment he presses the first 0, the field turns red with the words "Invalid input". Two things are wrong there. Guess first; the answer is at the end of this section.
When. Luke Wroblewski, a designer who wrote a book about forms, tested 22 people in 2009. He compared a form with inline validation (a message appears at each field while you fill it in) with a form that only checked after it was sent. Inline validation raised the success rate by 22%, cut errors by 22% and made filling in the form 42% faster. The best moment to show the message is after the guest leaves the field (on blur). A message that appears from the first character annoys people ("I haven't finished typing and it's already red"). A message that only appears after the submit button is pressed makes the guest scroll back up to find the wrong field.
What it says. An error message must say what's wrong and how to fix it (WCAG 3.3.1 and 3.3.3), in text, not only in colour. The WhatsApp number is accepted as 07... and +44 7.... The JavaScript below has two functions (named pieces of code that can be run). phoneProblem checks the number one rule at a time. It returns an error message, or empty text when the number is fine. check then shows that message under the field:
function phoneProblem(raw) {
const value = raw.trim();
if (value === "") return "Enter your WhatsApp number so Mrs. Sri can send the confirmation.";
if (!/^\+?[0-9\s.\-]+$/.test(value)) return "The number may only hold digits, spaces, dots, dashes and a + at the start.";
let digits = value.replace(/\D/g, ""); // drop spaces, dots, dashes and +
if (digits.startsWith("440")) return "After +44, drop the 0: write +44 7700 900123.";
if (digits.startsWith("44")) digits = "0" + digits.slice(2); // 447700... becomes 07700...
if (!digits.startsWith("07")) return "A mobile number starts with 07 or +44 7, for example 07700 900123.";
if (digits.length !== 11) return `This number is ${digits.length} digits. A mobile number starts with 07 and is 11 digits long.`;
return ""; // empty means no problem
}
const wa = document.querySelector("#wa");
const error = document.querySelector("#wa-error");
function check() {
const problem = phoneProblem(wa.value);
error.textContent = problem;
error.hidden = problem === "";
wa.setAttribute("aria-invalid", problem === "" ? "false" : "true");
}
wa.addEventListener("blur", check); // the first time: after leaving the field
wa.addEventListener("input", () => { if (!error.hidden) check(); }); // once an error shows: check on every keystroke
This is the result for a few numbers:
| Mr. Hadi types | Result |
|---|---|
07700-900123 |
accepted, 11 digits |
+44 7700 900123 |
accepted, becomes 07700... |
07700 90 |
"This number is 7 digits. A mobile number starts with 07 and is 11 digits long." |
+44 07700 900123 |
"After +44, drop the 0: write +44 7700 900123." |
020 7946 0123 |
"A mobile number starts with 07 or +44 7, for example 07700 900123." |
bluris the event (the moment) when focus leaves the field. The first error shows at that moment.- Once an error is showing, the
inputevent (on every keystroke) runscheckagain. So the message disappears as soon as the number is right, and the guest knows right away that the fix worked. aria-invalid="true"tells a screen reader that what is in the field is wrong.- Show the error in red
#b91c1c(6.47:1), plus an icon and text.
So the answer to the guess: the message appears too early (before Mr. Hadi has finished typing, and his number is actually fine), and it doesn't help ("Invalid input" doesn't say how to fix it).
Other options. For simple rules, the browser can do part of the work:
| Way | When to pick it |
|---|---|
Built-in attributes (required, type="email", minlength) plus the CSS input:user-invalid { outline: 3px solid #b91c1c; } |
simple rules, no JavaScript. :user-invalid exists since Chrome 119, Firefox 88 and Safari 16.5 |
setCustomValidity("message") then reportValidity() |
you want your own message, but shown in the browser's built-in bubble (a small message box the browser draws) |
Your own JavaScript with novalidate (the code above) |
rules the browser doesn't have, such as +44 becoming 07, and a message that stays written below the field. This lesson uses it, because the built-in bubble cannot be styled and looks different in every browser |
:user-invalid is a CSS selector for a field whose content is wrong after the user has touched it. It uses the same timing as the code above. We tried it in Chromium, Firefox and WebKit. The email field is not red while the guest types "abc", and turns red after they leave the field. An empty required field turns red only after the submit button is pressed.
⚠️ Small slips that make a form annoying
- A placeholder as the label. It disappears while typing, and its contrast is low. Use a
<label for>that points at the field'sid. forandidthat don't match.for="mail"withid="email": the connection breaks with no error message at all. In the exercise, everyforis checked: it must point at anidthat really exists.type="text"for email and phone numbers. The phone keyboard stays plain letters. Usetype="email"andtype="tel".- Errors from the first character, or only after submitting. Show them after the guest leaves the field, then remove them as soon as the content is right.
- "Invalid input". Name the problem and give a correct example.
TL;DR: Ask only for what's needed. Split a long form into steps, with the required fields in the first one. Put the fields one below the other, with a label (the field's name) above each one: a <label for> that exactly matches the field's id (its unique name). A placeholder (the example text inside a field) is not a label, because it disappears while typing. Use type="tel" and type="email" for the right keyboard, and autocomplete (name, tel, email, WCAG 1.3.5) so the browser can fill them in from data on the phone. Never use type="number" for a phone number. Mark required fields with required. Show an error message after the guest leaves the field (on blur), then check the field again on every keystroke until it's right. The message says what's wrong and how to fix it (WCAG 3.3.1, 3.3.3), with text and an icon, not only red. The WhatsApp number is accepted as 07... or +44 7..., 11 digits.
⚡ Ask only for what's needed
Meaning: Every extra field is one more question that can make someone stop. To book a room, Mrs. Sri only needs a name, a WhatsApp number and an optional email. A form that has to be long gets split into steps, with the required fields in the first one.
⚡ Labels on top, the right type
Write:
<label for="wa">WhatsApp number</label>
<input id="wa" name="wa" type="tel" autocomplete="tel" required aria-describedby="wa-help wa-error">
<p id="wa-help" class="help">Example: 07700 900123 or +44 7700 900123</p>
Meaning: <label> is the field's name, written above it. The label's for must exactly match the field's id (its unique name), here wa, so a screen reader (a program that reads the page aloud) says that name. aria-describedby connects the example text (wa-help) and the error message (wa-error) to the field, so they are read out too. A placeholder (the example text inside a field) isn't a label, because it disappears while typing. type="tel" brings up the phone keypad, and autocomplete="tel" fills it in from data on the phone with one tap. required marks the field as required. Never use type="number" for a phone number.
⚡ Errors after leaving the field
Write:
wa.addEventListener("blur", check); // the first time: after leaving the field
wa.addEventListener("input", () => { if (!error.hidden) check(); }); // once an error shows: check on every keystroke
Guess first: the number field turns red with "Invalid input" the moment Mr. Hadi presses the first 0. What are the two mistakes?
The message shows too early, before he has finished typing, and his number is actually fine. And the message doesn't help, because it doesn't say how to fix anything.
Meaning: wa is the WhatsApp number field, check checks the number and shows the error message, and error is the place for that message. The first line runs check on the blur event (the moment the guest leaves the field), so the first error shows then. The second line runs on every keystroke (the input event), but only runs check if !error.hidden, which means "the error message is showing". So after an error appears, the number is checked on every keystroke until it's right. The message says what's wrong and how to fix it: "This number is 7 digits. A mobile number starts with 07 and is 11 digits long."