: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

瀏覽器相容性

另見