The pseudo-classes :valid and :invalid.
Summary of characteristics of the :valid selector
Description of the pseudo-classes :valid and :invalid.
The pseudo-classes :valid and :invalid target elements whose value is valid and invalid, respectively. So here we're talking
about elements like input fields.
A value is considered valid if it meets the following conditions:
- It matches the data type defined in the HTML code (the
typeattribute of the input tag). - It matches the input format defined by the
patternattribute. - It falls within the range of allowed values set by the
minandmaxattributes. - It follows the
stepattribute which, along withminandmax, defines the allowed values.
For example,min="1" max="5" step="2"only allows the values1,3, and5. - The number of characters entered is within the range set by the
minlengthandmaxlengthattributes.
These two pseudo-classes can also be applied to forms. In this case, the selector :valid only kicks in if ALL the input fields in the form
are valid, and the selector :invalid only does if at least one of the input fields is invalid.
Other pseudo-classes let you target input areas based on more specific criteria:
:in-rangeand:out-of-rangetarget elements whose value is within the allowed range or outside of that range.:requiredand
:optionaltarget required input fields and optional ones, respectively.
Examples of using :valid and :invalid.
Example on input fields.
The selectors :valid and :invalid are applied both to the form itself and to the different input areas.
When all the elements of the form are valid, the form is also valid. If even a single area is invalid, the form is invalid.
Items with a valid value are outlined in green, and those with an invalid value are outlined in red.
- The "Postal Code" field is of the
texttype with a pattern that requires entering 5 digits. - The "Reference" field must have between 5 and 10 characters:
minlengthandmaxlengthattributes.
Also, this field is required (attributerequired). - The "Number" field is waiting for a numeric value (
type="number"). Most non-numeric characters are blocked, but it is still possible to enter incorrect values like5-5,5+5,e3, etc.
Since the minimum and maximum values have been set to10and20with a step of2, you can also trigger the pseudo-class:invalidby entering a value outside this range or an odd number. - The last two fields check that the input is in the correct format for an email address and a URL.
Example with input area groups fieldset.
The fieldset tag allows you to group input areas. Visually, this shows up as a rectangle around these areas, sometimes with a title.
When applied to a fieldset, the pseudo-classes :valid and :invalid work just like on a form: the fieldset
is valid if all the fields it contains are valid.
In the example below, both the username and password are required, and among the radio buttons you need to check one.
Browser compatibility.
The pseudo-classes :valid and :invalid don't cause any compatibility issues with current browsers.
:valid and :invalid that target form elements whose value is valid or invalid.:valid:invalidpseudo-class
Browsers on computers :
Mobile browsers :
Outdated or marginal browsers :

Internet Explorer

UC Browser pour Androïd

Opéra Mobile

QQ Browser

Baidu Browser

Samsung Internet

Edge

KaiOS Browser

Opéra

Firefox

Safari

Safari sur IOS

Chrome

Androïd Brower

Chrome pour Androïd

Firefox pour Androïd

Opéra mini
History of pseudo-classes :valid and :invalid.
-
Selectors Level 4
This specification module defines new selectors that can be used in CSS.
Regarding:validIntroduction of the:validand:invalidpseudo-classes.September 29, 2011Working Draft.
See also...
The pseudo-classes related to input areas and their state at a given moment (read-only, valid, required, etc.) are listed below. Check out their description to learn more.




select menu), and that is in the closed state.

disabled attribute).

select), and that are in the open state.









