Validity :valid / :invalid

The validity pseudo-classes match valid or invalid <input> or <form> elements.

An element without validity semantics is neither :valid nor :invalid.

:valid
The :valid pseudo-class represents an <input> element whose content validates. This tells the user that their input is valid.

:invalid
The :invalid pseudo-class represents an <input> element whose content does not validate. This tells the user that their input is invalid and must be changed.

Browser Support

Chrome Firefox Safari Opera IE Edge
Desktop yes yes yes yes yes yes
Mobile yes yes yes yes no yes

Syntax

E:valid { /* Style properties */ }

E:invalid { /* Style properties */ }

Example

/* In this example, all valid input fields will have a green outline, while invalid ones will have a red outline. */

input:valid {
outline: 1px solid green;
}

input:invalid {
outline: 1px solid red;
}

Information is based on Selectors Level 4 - W3C Working Draft (last updated on May 2, 2013)
Copyright © 2013 W3C® (MIT, ERCIM, Keio, Beihang), All Rights Reserved. W3C liability, trademark and document use rules apply.