:invalid

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 ⁨2015 年 7 月⁩以来,各浏览器均已提供此特性。

:invalid CSS 伪类表示任何<form><fieldset><input> 或其他内容验证失败的<form> 元素。

试一试

label {
  display: block;
  margin-top: 1em;
}

input:invalid {
  background-color: ivory;
  border: none;
  outline: 2px solid red;
  border-radius: 5px;
}
<form>
  <label for="email">Email Address:</label>
  <input id="email" name="email" type="email" value="na@me@example.com" />

  <label for="secret">Secret Code: (lower case letters)</label>
  <input id="secret" name="secret" type="text" value="test" pattern="[a-z]+" />

  <label for="age">Your age: (18+)</label>
  <input id="age" name="age" type="number" value="5" min="18" />

  <label
    ><input name="tos" type="checkbox" required checked /> - Do you agree to
    ToS?</label
  >
</form>

此伪类可用于向用户突出显示字段错误。

语法

css
:invalid {
  /* ... */
}

无障碍

红色常用于表示无效输入。患有某些类型色盲的人将无法确定输入的状态,除非它伴有不依赖颜色传达意义的附加指示器。通常,会使用描述性文本和/或图标。

示例

对元素着色以显示验证

HTML

html
<form>
  <div class="field">
    <label for="url_input">Enter a URL:</label>
    <input type="url" id="url_input" />
  </div>

  <div class="field">
    <label for="email_input">Enter an email address:</label>
    <input type="email" id="email_input" required />
  </div>
</form>

CSS

css
label {
  display: block;
  margin: 1px;
  padding: 1px;
}

.field {
  margin: 1px;
  padding: 1px;
}

input:invalid {
  background-color: #ffdddd;
}

form:invalid {
  border: 5px solid #ffdddd;
}

input:valid {
  background-color: #ddffdd;
}

form:valid {
  border: 5px solid #ddffdd;
}

input:required {
  border-color: maroon;
  border-width: 3px;
}

input:required:invalid {
  border-color: #c00000;
}

结果

分阶段显示部分

在此示例中,我们使用 :invalid~后续同级组合器)来使表单分阶段出现,因此表单最初显示要完成的第一项,当用户完成每项时,表单显示下一项。当整个表单完成时,用户可以提交它。

HTML

html
<form>
  <fieldset>
    <label for="form-name">Name</label><br />
    <input type="text" name="name" id="form-name" required />
  </fieldset>

  <fieldset>
    <label for="form-email">Email Address</label><br />
    <input type="email" name="email" id="form-email" required />
  </fieldset>

  <fieldset>
    <label for="form-message">Message</label><br />
    <textarea name="message" id="form-message" required></textarea>
  </fieldset>

  <button type="submit" name="send">Submit</button>
</form>

CSS

css
/* Hide the fieldset after an invalid fieldset */
fieldset:invalid ~ fieldset {
  display: none;
}

/* Dim and disable the button while the form is invalid */
form:invalid button {
  opacity: 0.3;
  pointer-events: none;
}

input,
textarea {
  box-sizing: border-box;
  width: 100%;
  font-family: monospace;
  padding: 0.25em 0.5em;
}

button {
  width: 100%;
  border: thin solid darkgrey;
  font-size: 1.25em;
  background-color: darkgrey;
  color: white;
}

结果

注意

单选按钮

如果组中的任何一个单选按钮是 required,并且组中没有选择任何按钮,则 :invalid 伪类将应用于所有按钮。(分组的单选按钮共享其 name 属性的相同值。)

Gecko 默认设置

默认情况下,Gecko 不会对 :invalid 伪类应用样式。但是,它确实会对 :user-invalid 伪类应用样式(使用 box-shadow 属性的红色“发光”),该伪类适用于 :invalid 的部分情况。

规范

规范
HTML
# selector-invalid
选择器 Level 4
# invalid-pseudo

浏览器兼容性

另见