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

IE8 compatibility notes I encountered

1. IE8's getElementById only supports id, not ...

CSS transparent border background-clip magic

This article mainly introduces the wonderful use ...

Summary of 3 minor errors encountered during MySQL 8.0 installation

Preface In the past, the company used the 5.7 ser...

Vue encapsulates the public function method of exporting Excel data

vue+element UI encapsulates a public function to ...

SQL statements in Mysql do not use indexes

MySQL query not using index aggregation As we all...

mysql 5.7.17 winx64.zip installation and configuration method graphic tutorial

Preface: I reinstalled win10 and organized the fi...

JS realizes simple picture carousel effect

This article shares the specific code of JS to ac...

How to create a trigger in MySQL

This article example shares the specific code for...

MySQL encoding utf8 and utf8mb4 utf8mb4_unicode_ci and utf8mb4_general_ci

Reference: MySQL character set summary utf8mb4 ha...

Problems and solutions for installing Docker on Alibaba Cloud

question When installing Docker using Alibaba Clo...

Getting Started with CSS3 Animation in 10 Minutes

Introduction Animation allows you to easily imple...

HTML head structure

The following introduces the commonly used head s...

Docker file storage path, modify port mapping operation mode

How to get the container startup command The cont...