Ladies and folks, are you doing form validation? Client or server, javascript or jquery, handwriting or using plug-ins, today we will explore using pure css to implement form validation, so as to learn the form-related pseudo-class selectors in css selectors level 4.
Case appreciation
I have also put the code in codepen, you can study it online, or download it for collection.
Knowledge Analysis
The key is to use some pseudo-classes in css selectors levle4 to implement form validation. These pseudo-classes are:
The above case uses: in-range and :out-of-range, let’s explain them one by one.
:required and :optional
:required can select form elements with the required attribute, which can be input, select, and textarea. For example, the following elements will be selected.
<input type="name" required><input type="checkbox" required><input type="email" required><!-- and other input types as well.. --><textarea name="name" id="message" cols="30" rows="10" required></textarea><select name="nm" id="sel" required> <!-- options --></select>
:optional selects form elements that do not have the required attribute. Using these two pseudo-classes, we can achieve the following interesting effect. The code is also placed in codepen. You can research it online or download it for collection. It’s up to you.
This code mainly uses two pseudo-classes: required and :optional to implement different styles and different prompt texts for the required and optional forms. The core code is shown below.
/*可选表单样式*/input:optional,select:optional { border-right: 3px solid #888; background-color: #f8f8f8; color: #888; }/*必选表单样式*/input:required,textarea:required { border-right: 3px solid #aa0088; }/*可选表单提示文字*/input:optional+label::after{ content:"(可选)"; }/*必选表单提示文字*/input:required+label::after{ content:"(必填)"; }/*可选表单激活效果*/input:optional:focus,select:optional:focus { box-shadow: 0 0 2px 1px #aaa; }/*必选表单激活效果*/input:required:focus,select:required:focus,textarea:required:focus { outline: 0; box-shadow: 0 0 2px 1px #aa0088; }
:in-range and out-of-range
These two pseudo-classes respectively select the form attribute value in the range and out-of-range states. These two pseudo-classes Can be used on elements that accept a range of numbers, such as forms or sliders of type number. The case effect is shown in the "Case Appreciation" section above. We only show the core code here to help everyone understand the use of the two pseudo-classes.
input:out-of-range{ border: 1px solid tomato; }input:in-range~ label::after { content: "输入一个正确的从1到10的数字"; }input:out-of-range ~ label::after { content: "枣糕,你傻了!"; }
:valid and :invalid
These two pseudo-classes are designed for input forms with type. For example, if there is a form with type=email, when its value is not a valid email address Triggered when the format is: invalid pseudo-class, triggered when the value is a valid email format: valid pseudo-class.
Similarly, it is placed in codepen, please study online or download the collection, and then we will take a look at the core code, as shown below.
input:invalid{ border: 1px solid tomato; }input:valid~ label::after { content: "耶,一个邮箱!"; }input:invalid ~ label::after { content: "枣糕,邮箱邮箱,是邮箱吗?"; }
:read-only and :read-write
The following elements can activate the :read-only pseudo-class.
For example, below The elements shown in the code can activate the :read-only pseudo-class selector.
<input type="text" disabled><input type="number" disabled><input type="number" readonly><textarea name="nm" id="id" cols="30" rows="10" readonly> </textarea><div class="random"> </div> <!-- regular element that is not editable with contenteditable -->
: The read-write element is exactly the opposite of the :read-only element.
This is relatively simple, so I won’t do a case anymore. Thanks.
In-depth reading
Acknowledgments

HTMLtagsdefinethestructureofawebpage,whileattributesaddfunctionalityanddetails.1)Tagslike,,andoutlinethecontent'splacement.2)Attributessuchassrc,class,andstyleenhancetagsbyspecifyingimagesources,styling,andmore,improvingfunctionalityandappearance.

The future of HTML will develop in a more semantic, functional and modular direction. 1) Semanticization will make the tag describe the content more clearly, improving SEO and barrier-free access. 2) Functionalization will introduce new elements and attributes to meet user needs. 3) Modularity will support component development and improve code reusability.

HTMLattributesarecrucialinwebdevelopmentforcontrollingbehavior,appearance,andfunctionality.Theyenhanceinteractivity,accessibility,andSEO.Forexample,thesrcattributeintagsimpactsSEO,whileonclickintagsaddsinteractivity.Touseattributeseffectively:1)Usese

The alt attribute is an important part of the tag in HTML and is used to provide alternative text for images. 1. When the image cannot be loaded, the text in the alt attribute will be displayed to improve the user experience. 2. Screen readers use the alt attribute to help visually impaired users understand the content of the picture. 3. Search engines index text in the alt attribute to improve the SEO ranking of web pages.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML is used to build web page structure; 2. CSS is used to beautify the appearance of web pages; 3. JavaScript is used to achieve dynamic interaction. Through tags, styles and scripts, these three together build the core functions of modern web pages.

Setting the lang attributes of a tag is a key step in optimizing web accessibility and SEO. 1) Set the lang attribute in the tag, such as. 2) In multilingual content, set lang attributes for different language parts, such as. 3) Use language codes that comply with ISO639-1 standards, such as "en", "fr", "zh", etc. Correctly setting the lang attribute can improve the accessibility of web pages and search engine rankings.

HTMLattributesareessentialforenhancingwebelements'functionalityandappearance.Theyaddinformationtodefinebehavior,appearance,andinteraction,makingwebsitesinteractive,responsive,andvisuallyappealing.Attributeslikesrc,href,class,type,anddisabledtransform

TocreatealistinHTML,useforunorderedlistsandfororderedlists:1)Forunorderedlists,wrapitemsinanduseforeachitem,renderingasabulletedlist.2)Fororderedlists,useandfornumberedlists,customizablewiththetypeattributefordifferentnumberingstyles.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools
