CSS form validation function implementation code

CSS form validation function implementation code

Rendering

principle

In the form element, there is a pattern attribute, which can customize regular expressions (such as mobile phone number, email address, ID card, etc.); the valid pseudo-class can match elements that pass the pattern validation; the invalid pseudo-class, on the contrary, can match elements that fail the pattern validation; so you can do whatever you want. The effect diagram above only makes some simple effects. For more effects and restrictions, you can use your imagination;

html

The layout is very simple. Input and button are sibling nodes. The required attribute means that the input content must be verified.

<section class="container">
  <input type="text" name="tel" placeholder="Please enter your mobile number" pattern="^1[3456789]\d{9}$" required><br>
  <input type="text" name="tel" placeholder="Please enter the verification code" pattern="\d{4}" required><br>
  <button type="submit"></button>

CSS

The scss preprocessor is used here

input {
  //Button style when validation passes&:valid {
    &~button {
      pointer-events: all;
      cursor: pointer;
      &::after {
        content: "Submit: +1:"
      }
    }
  }
  //Button style when validation fails&:invalid {
    &~button {
      pointer-events: none; // Remove click events to make the button unclickable&::after {
        content: "Unverified:unamused:"
      }
    }
  }
}

Summarize

The above is the implementation code of the CSS form validation function introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to you in time. I would also like to thank everyone for their support of the 123WORDPRESS.COM website!
If you find this article helpful, please feel free to reprint it and please indicate the source. Thank you!

<<:  Key issues and solutions for web page access speed

>>:  Detailed explanation of the application of the four states of hyperconnection

Recommend

Common date comparison and calculation functions in MySQL

Implementation of time comparison in MySql unix_t...

Solution to inconsistent display of cursor size in input box

The cursor size in the input box is inconsistent T...

Detailed explanation of basic interaction of javascript

Table of contents 1. How to obtain elements Get i...

Detailed discussion of the differences between loops in JavaScript

Table of contents Preface Enumerable properties I...

How to monitor Windows performance on Zabbix

Background Information I've been rereading so...

Solution to incomplete text display in el-tree

Table of contents Method 1: The simplest way to s...

Using jQuery to implement the carousel effect

This article shares the specific code for impleme...

Detailed process of building nfs server using Docker's NFS-Ganesha image

Table of contents 1. Introduction to NFS-Ganesha ...

How to set the number of mysql connections (Too many connections)

During the use of mysql, it was found that the nu...

Docker custom network detailed introduction

Table of contents Docker custom network 1. Introd...

Swiper.js plugin makes it super easy to implement carousel images

Swiper is a sliding special effects plug-in built...