大网数据广告活动
Web 标准与前端开发

ARIA: aria-required attribute

· 52CE 我爱测

The `aria-required` attribute indicates that user input is required on the element before a form may be submitted.

The aria-required attribute indicates that user input is required on the element before a form may be submitted.

Description

When a semantic HTML input, select, or textarea must have a value, it should have the required attribute applied to it. The HTML required attribute disables submitting the form unless the required form controls have valid values, while ensuring those navigating with the aid of assistive technologies understand which semantic form controls need valid content.

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. The aria-required attribute can be used with HTML form elements; it is not limited to elements that have an ARIA role assigned.

Similar to the HTML required attribute set on semantic HTML form controls, the aria-required attribute explicitly conveys to assistive technologies that the element is required before a form may be submitted. The required attribute on a semantic HTML form control will prevent the form control from being submitted if no value is present — providing native error messaging in some browsers if a required value is invalid when the user attempts to submit the form. The aria-required attribute, like all ARIA states and properties, has no impact on element functionality. Functionality and behavior must be added in with JavaScript.

[!NOTE] ARIA only modifies the accessibility tree, modifying how assistive technology presents content to users. ARIA does not change anything about an element's function or behavior. When not using semantic HTML elements for their intended purpose and default functionality, you must use JavaScript to manage behavior, focus, and ARIA states.

The CSS :required and :optional pseudoclasses match input, select, and textarea elements based on whether they are required or optional, respectively. When using non-semantic elements as form controls, you don't get this CSS pseudoclass selector benefit. You can, however, use attribute selectors if the attribute is present: [aria-required="true"] or [aria-required="false"].

If a form contains both required and optional form elements, the required elements should be indicated visually using a treatment that does not rely solely on color to convey meaning. Typically, descriptive text and/or an icon are used.

[!NOTE] Which elements are required should be apparent to all users. Ensure the visual presentation indicates the form control is required in a consistent, visible manner, remembering that color is not enough to convey information.

Examples

The attribute should be added to the form-control role. If the user needs to fill in an email address textbox, include aria-required="true" on the textbox.

<div id="tbLabel">Email Address *</div>
<div
  role="textbox"
  contenteditable
  aria-labelledby="tblabel"
  aria-required="true"
  id="email1"></div>

[!NOTE] If the field's label already contains the word "required", it is recommended to leave out the aria-required attribute. This avoids that screen readers read out the term "required" twice.

In this example, JavaScript must be used to prevent the containing form from being submitted if the textbox has no content.

This could be written semantically, without the need for JavaScript:

<label for="email1">Email Address (required)</label>
<input type="email" id="email1" required />

Values

  • true
    • : The element requires a value or must be checked for the form to be submittable.
  • false
    • : The element is not required.

Associated interfaces

  • Element.ariaRequired
    • : The ariaRequired property, part of the Element interface, reflects the value of the aria-required attribute.
  • ElementInternals.ariaRequired
    • : The ariaRequired property, part of the ElementInternals interface, reflects the value of the aria-required attribute.

Associated roles

Used in roles:

Inherits into roles:

Specifications

See also

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。