Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do I show a form's required fields clearly in its markup?

Quick answer

For a supported form control, add the HTML required attribute so it must have a value before submission. Also show people that it is required, for example with “(required)” in its associated label. Associate the label with the input using matching for and id values or by nesting. Don’t rely on color alone or use placeholder text instead of a label. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I’m marking up a form with a few mandatory fields. What should I put in the HTML so the fields are actually required, and what should a person see?

Geez

For a supported control, use the Boolean required attribute. It indicates that the control must have a value before the form can be submitted. [S1]

Also give the field an associated label and include a visible cue, such as “(required)” in the label. For example, this assumes a short form asking for a name:

<label for="full-name">Full name (required)</label>
<input id="full-name" name="full-name" type="text" required>

The matching for and id associate the label with the input. You can also associate them by putting the input inside its label. [S2] The visible words tell people the field is required without relying on color alone.

Follow-up question

Could I just put an asterisk beside the field and color it red? I’d rather not add “required” to every label.

Geez

An asterisk can serve as the visible cue if you explain its meaning in visible instructions, for example, “* Required.” Don’t make color the only way to identify required fields; communicate the requirement through more than color alone. [S1]

If you use a form-wide convention, explain it where people can see it while completing the form, and keep the cue near each required field. A note beside an input does not replace an associated label: the label should still identify the field. [S2]

Follow-up question

Does aria-required="true" do the same thing as required? And can I use placeholder text instead of a label?

Geez

No. The HTML required attribute makes a supported control mandatory for submission. aria-required="true" communicates required status to assistive technology, but does not itself make the field mandatory. [S1] Use required for the submission requirement; don’t use ARIA as its replacement.

Placeholder text is not a substitute for a label. Use an associated <label> to identify the field, and use placeholder text only as optional supplementary guidance if useful. [S2] In the example above, the label names the field and says it is required, while the input has the required attribute.

Follow-up question

What should I check before publishing? I want to make sure the markup and the visible instructions tell the same story.

Geez

Review the form one field at a time. For every mandatory control, check that the HTML requirement and visible wording agree, and that its label is properly associated with it. If you use an asterisk convention, make sure its meaning is explained and the cue is not conveyed by color alone.

For example, if the instructions say “Fields marked * are required,” a required name field could use the label Full name * and an input with required. That is one possible convention, not a requirement to use an asterisk. The cited guidance supports using the required attribute, a visible indication, and an associated label; it does not specify how every browser or assistive technology will present a particular form. [S1] [S2]

A practical checklist

  • Add required to each supported control that must have a value before submission.

  • Give each field an associated <label>; match its for value to the input’s id, or nest the input in the label.

  • Show a visible cue such as “(required)” or an asterisk explained in visible instructions.

  • Do not use color alone to distinguish required fields.

  • Do not use placeholder text as a replacement for a label.

  • Check that visible instructions and fields marked required agree.

A few more useful answers

Is `aria-required="true"` enough to make a field mandatory?

No. It communicates required status to assistive technology but does not itself make the field mandatory; use the HTML required attribute for that. [S1]

Can placeholder text serve as the field’s label?

No. Use an associated <label> to identify the field; placeholder text is not a substitute. [S2]

Do I have to write “required” instead of using an asterisk?

You can use an asterisk as a visible cue if you explain its meaning in visible instructions. Don’t rely on color alone. [S1]

Sources

References used for this guide.

  1. [S1] The required HTML attribute
    developer.mozilla.org · Checked
  2. [S2] The HTML input element: Labels and accessible names
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work