Sub topic 7
Marking Required and Optional Fields
This chapter assumes you know basic HTML forms and CSS. You should also know how labels and hint text work, which earlier chapters covered. Here you will learn how to tell users which fields they must complete, and how to make that signal reach every user.
Start by cutting, then mark what remains
Each field you keep needs a reason. A field that is truly optional is often a field you can remove. After cutting, mark the remaining fields in one consistent way so users never have to guess.
Choose one convention
There are two common conventions. Pick the one that matches your form.
| Situation | What to mark | Example label |
|---|---|---|
| Most fields are required | Mark the optional ones | "Phone number (optional)" |
| Most fields are optional | Mark the required ones | "Email address (required)" |
Marking the minority keeps the page quiet. If every label carries the same note, the note stops carrying information.
Write the marker as text in the label. Do not rely on an asterisk alone. An asterisk means nothing until a legend explains it. Screen readers may skip it or read it as "star". If you do use an asterisk, explain it near the top of the form, and consider including the word "required" too.
Mark fields in the HTML too
Visible text helps sighted users. The required attribute tells the browser and assistive technology that the field must be filled in. Use both.
<label for="name">Full name (required)</label>
<input id="name" name="name" type="text" autocomplete="name" required>
<label for="phone">Phone number (optional)</label>
<input id="phone" name="phone" type="tel" autocomplete="tel">When a field has required, screen readers announce it as required. Because the label also says "(required)", some readers may hear it twice. This is a small cost. Hearing it twice is better than not hearing it at all, and many accessibility guides accept it.
If you validate with your own script and set novalidate on the form, the browser will not show its built-in messages. The required attribute still helps: it keeps the field exposed as required, and validity.valueMissing still works. You can also add aria-required="true" on a control that is not a native input, such as a custom widget.
Be consistent across the whole form
Use the same wording, position, and style on every field. Put the marker at the end of the label text, inside the label element, so it is linked to the field and clicking it focuses the input.
Common mistakes:
- Marking some required fields with an asterisk and others with the word "required".
- Placing "(optional)" in a placeholder. The placeholder vanishes when the user types.
- Switching convention between the steps of a multi-step form.
- Marking a field required in the interface but accepting an empty value on the server, or the reverse.
That last point matters. Client-side marking is guidance. The server must enforce the same rules.
A field that depends on another
Sometimes a field is required only after another choice. For example, a phone number is required if the user chose "Phone" as their contact method. Update the label and the required attribute together when the choice changes.
const phoneRadio = document.getElementById('contact-phone');
const phone = document.getElementById('phone');
const phoneLabel = document.querySelector('label[for="phone"]');
document.querySelectorAll('input[name="contact"]').forEach((radio) => {
radio.addEventListener('change', () => {
const needed = phoneRadio.checked;
phone.required = needed;
phoneLabel.textContent = needed
? 'Phone number (required)'
: 'Phone number (optional)';
});
});This keeps the visible text and the programmatic state in step. Mismatches between the two confuse screen reader users.
Exercise
A checkout form has ten fields. Nine are required and one, "Delivery notes", is not. Which convention should you use, how would you write the label for "Delivery notes", and why should you also add the required attribute to the other nine inputs?
Show answer
Mark the minority, so mark the optional field: "Delivery notes (optional)". The other nine need no visible note. Add required to those nine so the browser and screen readers know they must be filled in, and so validity.valueMissing works in your validation script. Enforce the same rules on the server.