Sub topic 7
Marking Required and Optional Fields
This chapter assumes you can write basic HTML forms and know how label, for and id connect a label to a control, which earlier chapters covered. The last section uses a little JavaScript. Here you will decide how to tell users which fields they must fill in, and then implement that so the browser, screen readers and sighted users all get the same information.
Why marking matters
Users decide whether to start a form, and how carefully to fill it in, based on what it asks of them. If nothing says which fields are mandatory, people either fill in everything, which wastes effort, or skip fields and hit errors after submitting. Both outcomes cost completions.
Marking is a communication problem with two audiences:
- Sighted users need a visible cue in or near the label.
- Assistive technology users need the required state exposed programmatically, in the accessibility tree, not only as a picture of a star.
A good solution serves both without duplicating or contradicting itself.
Choosing a convention: mark required or mark optional
There are two honest conventions. Pick one per form, and ideally one per product.
| Convention | Visible cue | Works best when | Risk |
|---|---|---|---|
| Mark required | Asterisk or "(required)" on mandatory fields | Most fields are optional | A missed marker looks like "optional" |
| Mark optional | "(optional)" on non-mandatory fields | Most fields are required, which is common | Users may not notice the word and assume the field is mandatory |
The count of fields decides it. If a form has eight fields and seven are mandatory, adding "(optional)" to one label is quieter than adding seven asterisks. The first chapter of this tutorial argued for removing unneeded fields, and this follows from it. If you find yourself marking many fields as optional, ask whether those fields should exist at all.
Do not mix the two conventions in one form. A form with some asterisks and some "(optional)" labels forces users to work out which rule applies to an unmarked field.
Marking in text, not in color
Text is the most reliable marker. The word "optional" in a label needs no explanation, is read aloud by screen readers as part of the label, and survives high-contrast modes and printing.
<form action="/subscribe" method="post">
<div>
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required>
</div>
<div>
<label for="company">Company name (optional)</label>
<input id="company" name="company" type="text" autocomplete="organization">
</div>
<button type="submit">Subscribe</button>
</form>Here the email field carries the required attribute and the company field says "(optional)". Because the label text is the accessible name, a screen reader announces "Company name (optional), edit text". The email field is announced as "Email address, edit text, required", because the browser maps required to the accessibility tree.
Keep the marker inside the label element. A marker placed in a separate span next to the input is not part of the accessible name and may be skipped.
The asterisk pattern
An asterisk is compact and widely recognized, but it has three conditions:
- Explain it before the first field, for example "Fields marked with an asterisk (*) are required." Users reading top to bottom meet the key before the symbol.
- Do not rely on color. A red asterisk is fine, but it must be a visible character, and its contrast against the background must meet the 4.5:1 text ratio.
- Avoid double announcement. Some screen readers read an asterisk as "star". If
requiredalready conveys the state, hide the symbol from assistive technology.
<p>Fields marked with an asterisk (*) are required.</p>
<form action="/contact" method="post">
<label for="name">Full name <span aria-hidden="true">*</span></label>
<input id="name" name="name" type="text" autocomplete="name" required>
<label for="message">Message <span aria-hidden="true">*</span></label>
<textarea id="message" name="message" rows="5" required></textarea>
<button type="submit">Send</button>
</form>The aria-hidden="true" on the star removes it from the accessibility tree. Screen reader users hear "required" from the attribute instead of "star". The explanatory sentence is plain text, so it is not hidden.
A cleaner alternative is the word "required" in the label, which needs no key and no hiding trick. It is longer, so it suits forms with few required fields.
Programmatic state: required versus aria-required
The required attribute does two jobs on native controls. It exposes the required state to assistive technology, and it makes the browser block submission and show its own message when the field is empty.
aria-required="true" does only the first job. It is useful in two cases:
- A custom widget built from
divelements with an ARIA role, such as a custom listbox, whererequiredhas no effect. - A form with
novalidate, where you handle validation yourself and want to keep the browser from showing its own bubbles. Many teams still keeprequiredin this case, becausenovalidateonly suppresses the bubbles, whilerequiredcontinues to inform assistive technology andcheckValidity()still reportsvalueMissing.
Do not add aria-required="true" to a control that already has required. It is redundant and gives you two attributes to keep in sync.
| Situation | Use |
|---|---|
| Native input, select or textarea | `required` |
| Native control, you handle validation yourself | `required` plus `novalidate` on the form |
| Custom widget with an ARIA role | `aria-required="true"` |
Groups: radios and checkboxes
For a radio group, the question belongs to the fieldset and legend, so the marker belongs in the legend. Setting required on one radio in a group makes the whole group required for native validation, but the convention is to put it on every radio so the state is announced on whichever one receives focus.
<fieldset>
<legend>Preferred contact method (optional)</legend>
<div>
<input type="radio" id="contact-email" name="contact" value="email">
<label for="contact-email">Email</label>
</div>
<div>
<input type="radio" id="contact-phone" name="contact" value="phone">
<label for="contact-phone">Phone</label>
</div>
</fieldset>A single checkbox, such as "I accept the terms", is required by adding required to the input. A set of checkboxes, such as "choose at least one topic", has no native "at least one" rule. The required attribute on each box would force all of them. For that case you need a script and a message on the group, and you should state the rule in hint text, for example "Choose at least one."
Edge cases and common errors
- Conditionally required fields. If "Phone number" is needed only when the user picks "Phone" as the contact method, toggle
requiredand the visible marker together when the choice changes. A field that is visibly optional but fails validation is a trap. - Hidden required fields. A
requiredcontrol inside a hidden panel blocks submission, and the browser cannot focus it to show a message. Removerequiredor disable the control when you hide it. - Marker only in the placeholder. A placeholder such as "Required" vanishes on typing and is not a reliable label. Hint text and labels carry this information, as the earlier chapter on placeholders explained.
- Inconsistent wording. Using "(required)" on one page and a star on another makes users relearn the rule. Define the pattern once in your component library.
- No marker on a field the server requires. If the server rejects an empty field, the field must have been marked required in the form. Keep the front-end markup and back-end rules in step.
- Required means "not empty", not "valid". A required email field still needs the right
typeand a clear error message, both covered in later chapters.
A short script for conditional requirements
// Assumes radios named "contact" and a phone input with id="phone"
// whose label is <label for="phone">Phone number <span id="phone-marker" aria-hidden="true"></span></label>.
const radios = document.querySelectorAll('input[name="contact"]');
const phone = document.getElementById('phone');
const marker = document.getElementById('phone-marker');
function updatePhoneRequirement() {
const needsPhone = document.querySelector('input[name="contact"]:checked')?.value === 'phone';
phone.required = needsPhone;
marker.textContent = needsPhone ? '*' : '';
}
radios.forEach((radio) => radio.addEventListener('change', updatePhoneRequirement));
updatePhoneRequirement();Setting phone.required updates the accessibility tree and native validation in one step, and the marker text follows it. Both change from a single function, so they cannot drift apart.
Exercise
A checkout form has ten fields. Nine are mandatory and one, "Delivery notes", is not. Which marking convention fits, and what do you put on the controls?
Show answer
Mark the optional field, because most fields are required. Write "Delivery notes (optional)" in the label, and add the required attribute to the nine mandatory controls so the state is exposed to assistive technology and used by native validation.
Exercise
You mark a field with <span aria-hidden="true">*</span> inside its label, but the input has no required or aria-required attribute. What does a screen reader user hear, and how do you fix it?
Show answer
They hear only the label text, with no indication that the field is required, because the hidden star is skipped. Add required to the input (or aria-required="true" on a custom widget).
Exercise
A "Phone number" field becomes mandatory only when the user selects "Phone" as the contact method. What two things must change when that choice is made?
Show answer
The required state of the input and the visible marker (asterisk or "(optional)" text) must change together, so the visible cue, the accessibility tree and the validation all agree.