The Boolean
required
属性
which, if present, indicates that the user must specify a value for the input before the owning form can be submitted. The
required
attribute is supported by
text
,
search
,
url
,
tel
,
email
,
password
,
date
,
month
,
week
,
time
,
datetime-local
,
number
,
checkbox
,
radio
,
file
,
<input>
types along with the
<select>
and
<textarea>
form control elements. If present on any of these input types and elements, the
:required
pseudo class will match. If the attribute is not included, the
:optional
pseudo class will match.
The attribute is not supported or relevant to
range
and
color
, as both have default values. It is also not supported on
hidden
as it can not be expected that a user to fill out a form that is hidden. Nor is it supported on any of the button types, including
image
.
注意
color
and
range
don't support
required
, but type
color
默认为
#000000
,和
range
defaults to the midpoint between
min
and
max
-- with
min
and
max
defaulting to 0 and 100 respectively in most browsers if not declared -- so always has a value.
When an input has the
required
attribute, the
:required
pseudo-class also applies to it. Conversely, inputs that support the
required
attribute but don't have the attribute set match the
:optional
pseudo-class.
In the case of a same named group of
radio
buttons, if a single radio button in the group has the
required
attribute, a radio button in that group must be checked, although it doesn't have to be the one with the attribute is applied. So to improve code maintenance, it is recommended to either include the
required
attribute in every same-named radio button in the group, or else in none.
In the case of a same named group of
checkbox
input types, only the checkboxes with the
required
attribute are required.
Note: Setting
aria-required
="true"
tells a screen reader that an element (any element) is required, but has no bearing on the optionality of the element.
Because a read-only field cannot have a value,
required
does not have any effect on inputs with the
readonly
attribute also specified.
When including the
required
attribute, provide a visible indication near the control informing the user that the
<input>
,
<select>
or
<textarea>
is required. In addition, target required form controls with the
:required
pseudo-class, styling them in a way to indicate they are required. This improves usability for sighted users. Assistive technology should inform the user that the form control in mandatory based on the required attribute, but adding
aria-required
="true"
doesn't hurt, in case the browser / screen reader combination does not support
required
yet.
If the element is required and the element's value is the empty string, then the element is suffering from
valueMissing
and the element will match the
:invalid
pseudo class.
Provide an indication to users informing them the form control is required. Ensure the messaging is multi-faceted, such as thru text, color, markings, and attribute, so that all users understand the requirements whether they have color blindness, cognitive differences, or are using a screen reader.
<form>
<div class="group">
<input type="text">
<label>Normal</label>
</div>
<div class="group">
<input type="text" required="required">
<label>Required</label>
</div>
<input type="submit">
</form>
No compatibility data found. Please contribute data for "html.elements.attributes.required" (depth: 1) to the MDN 兼容性数据存储库 .
<a>
<abbr>
<acronym>
<address>
<applet>
<area>
<article>
<aside>
<audio>
<b>
<base>
<basefont>
<bdi>
<bdo>
<bgsound>
<big>
<blink>
<blockquote>
<body>
<br>
<button>
<canvas>
<caption>
<center>
<cite>
<code>
<col>
<colgroup>
<command>
<content>
<data>
<datalist>
<dd>
<del>
<details>
<dfn>
<dialog>
<dir>
<div>
<dl>
<dt>
<element>
<em>
<embed>
<fieldset>
<figcaption>
<figure>
<font>
<footer>
<form>
<frame>
<frameset>
<h1>
<head>
<header>
<hgroup>
<hr>
<html>
<i>
<iframe>
<image>
<img>
<input>
<ins>
<isindex>
<kbd>
<keygen>
<label>
<legend>
<li>
<link>
<listing>
<main>
<map>
<mark>
<marquee>
<menu>
<menuitem>
<meta>
<meter>
<multicol>
<nav>
<nextid>
<nobr>
<noembed>
<noframes>
<noscript>
<object>
<ol>
<optgroup>
<option>
<output>
<p>
<param>
<picture>
<plaintext>
<pre>
<progress>
<q>
<rb>
<rp>
<rt>
<rtc>
<ruby>
<s>
<samp>
<script>
<section>
<select>
<shadow>
<slot>
<small>
<source>
<spacer>
<span>
<strike>
<strong>
<style>
<sub>
<summary>
<sup>
<table>
<tbody>
<td>
<template>
<textarea>
<tfoot>
<th>
<thead>
<time>
<title>
<tr>
<track>
<tt>
<u>
<ul>
<var>
<video>
<wbr>
<xmp>
<input>
类型
<input type="button">
<input type="checkbox">
<input type="color">
<input type="date">
<input type="datetime">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="hidden">
<input type="image">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="reset">
<input type="search">
<input type="submit">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<input type="week">