Original article: Pure CSS to achieve a cool input box effect
See the effect first
:valid and :invalid pseudo-classes
These two pseudo-classes can be used on input and select elements to select input boxes that have passed or failed validation. Let’s learn about them through the following simple example:
This input box will check whether the input is an email address, otherwise the background color will be red warning.
The code is also very simple:
|
|
required attribute
This attribute can be used on input and select elements to indicate that this input box is required. If left blank, it will fail the validation and can be selected by the :invalid pseudo-class, otherwise it will be selected by :valid, for example:
This input box cannot be left blank, otherwise the background color will be red warning.
|
|
pattern attribute
This attribute can be used on input and select elements. Its attribute value needs to be filled in with a regular expression. If the content of the input box does not match the regular expression, it can be selected by the :invalid pseudo-class, otherwise it will be selected by :valid. For example:
This input box can only enter six digits, otherwise the background color will be red warning.
|
|
Complete code
|
|
Related knowledge
Form data validation
:valid pseudo-class
:invalid pseudo-class
required attribute
pattern attribute