HTML Guide
The aria-required
attribute is used to indicate to screen reader users that a form input is required. As there is now in HTML a general required
attribute which works with most user agents, it’s unnecessary to use both at the same time. In general, you can rely solely on the required
attribute, unless you want to provide backwards compatibility on old screen reader software versions.
Example:
<form action="order.">
<!-- This will raise a warning on unnecesary attributes -->
<input id="city" name="city" aria-required="true" required />
<!-- You can use this instead -->
<input id="city" name="city" required />
</form>
Learn more:
Related W3C validator issues
The W3C HTML validator issue arises when a <select> element is marked as required but doesn’t contain any <option> elements. The semantic purpose of a <select> element is to provide a list of options for the user to select from. If it is marked as required, it implies the user must make a selection from available options.
Here is an explanation of the involved elements and attributes:
-
<select>: This represents a drop-down list in an HTML form.
- required: When this attribute is present, it means the user must select an option before submitting the form. It cannot be empty.
- multiple: This boolean attribute allows multiple selections if present.
- size: This attribute specifies the number of visible options. If greater than 1 and multiple is not set, it turns the drop-down into a list box.
If the multiple attribute is not present and the size attribute is not greater than 1, you need at least one <option> element for the <select> to be valid when required is used.
To resolve this, ensure that your <select> element contains at least one <option>:
<label for="choices">Choose an option:</label>
<select id="choices" name="choices" required>
<option value="">--Please choose an option--</option>
<option value="option1">Option 1</option>
<option value="option2">Option 2</option>
</select>
This example includes a placeholder option with an empty value to prompt the user to select an option and other valid options. The value for the placeholder is empty, which is a common practice for required fields to prevent it from being selected by default.
The boolean required attribute can only be used with certain types of inputs. Check the input type is one of the allowed.
The required attribute, if present, indicates that the user must specify a value for the input before the owning form can be submitted.
The only accepted value for the aria-required property is true.
The aria-required attribute indicates that user input is required on the element before a form may be submitted.
When a semantic HTML <input>, <select>, or <textarea> must have a value, it should have the required attribute applied to it. When form controls are created using non-semantic elements, such as a <div> with a role of checkbox, the aria-required attribute should be included, with a value of true, to indicate to assistive technologies that user input is required on the element for the form to be submittable.
Example:
<div id="email_label">Email Address *</div>
<div
role="textbox"
contenteditable
aria-labelledby="email_label"
aria-required="true"
id="email"></div>
The boolean required attribute, if present, indicates that the user must specify a value for the input before the owning form can be submitted.
If this property is not present, the input will be considered as optional. To mark an input as required, it’s enough to include the property without any value, or pass it the required value as in these examples:
Example:
<input type="text" required>
<input type="text" required="required">
Using aria-required on a div element requires more context about that element, by providing one of the attributes aria-expanded, aria-valuemax, aria-valuemin, aria-valuenow, role.
When possible, you should prefer semantic tags like input, select and textarea, and use the required attribute, but when form controls are created using non-semantic elements, such as a div element, the aria-required attribute should be included with a value of true to indicate to assistive technologies that user input is required on the element for the form to be submittable. In that case, other attributes might be needed to make the element valid.
For example, we could build a radiogroup using a div like this:
<div aria-required="true">
<div data-value="One"></div> 1
<div data-value="Two"></div> 2
<div data-value="Three"></div> 3
</div>
This HTML snippet would then be decorated using CSS and added functionality using JavaScript. However, the W3C validator will complain that the element div is missing one of the attributes aria-expanded, aria-valuemax, aria-valuemin, aria-valuenow, role.
We can fix that by adding the appropiate role to that div element, like this:
<div aria-required="true" role="radiogroup">
<div data-value="One"></div> 1
<div data-value="Two"></div> 2
<div data-value="Three"></div> 3
</div>
Here is an example showing how to add the “role” attribute to the “div” element:
<div role="region">
<!-- Your content goes here -->
</div>
In this example, the role attribute is added with the value radiogroup. You can choose the appropriate ARIA role based on the purpose or role of your div element.
Remember to also provide the necessary values for the specified attributes if you are adding aria-valuemax, aria-valuemin, or aria-valuenow to ensure proper accessibility and usability of your content.