jQuery implements form validation function

jQuery implements form validation function

jQuery form validation example / including username, password, address, email verification

As shown below

Don't forget to import the jQuery framework! ! !

Without further ado, let's get straight to the jQuery code:

<script type="text/javascript">
 $(document).ready(function(){
  var tip1 = "<span class='span1'>Username cannot be empty!</span>"; //Span added after the input box when an error occurs
  var tip2 = "<span class='span2'>The email format is incorrect or cannot be empty!</span>";
  var tip3 = "<span class='span3'>Address cannot be empty!</span>";
  var tip4 = "<span class='span4'>The password length cannot be less than five characters and can be at most ten characters!</span>";
  var condition = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/; //Declare the condition for determining the email format $(".id").blur(function(){
   if(!$(".id").val()){//Judge whether the user name is not empty$(".span1").remove();
    $(".id").after(tip1);
   }
   else{
    $(".span1").remove();
   }
  });
  $(".email").blur(function(){
   
   if(!condition.test($(".email").val())){//Determine the email format$(".span2").remove();
    $(".email").after(tip2);
   }
   else{
    $(".span2").remove();
   }

  });
  $(".adress").blur(function(){
   if(!$(".adress").val()){//Judge whether the address is not empty$(".span3").remove();
    $(".adress").after(tip3);
   }
   else{
    $(".span3").remove();
   }
  });
  $(".pwd").blur(function(){
   
   if($(".pwd").val().length < 5||$(".pwd").val().length >10){//Determine that the password length cannot be less than 5 digits and cannot be greater than 10 digits$(".span4").remove();
    $(".pwd").after(tip4);
   }
   else{
    $(".span4").remove();
   }
  });
  $(".button").click(function(){//If all conditions are met, a pop-up window will pop up to verify that the form has passed. If not, a pop-up window will pop up to remind the user to change if(!$(".id").val()||!condition.test($(".email").val())||!$(".adress").val()||$(".pwd").val().length < 5||$(".pwd").val().length >10){
    alert("The registration information is incorrect, please change your personal information");
   }
   else{
    alert("Registration successful");
   }
  })
 })
</script>

Structure and style:

<div class="main_box">
  <div class="title">
   Welcome to register Yuanmo</div>
  <div class="box">
  <img alt="Illustration" src="./img/Kelipai Meng.png" class="img">
   <form>
    Username:<input class="id" type="text" ><br>
    Email:<input class="email" type="text"><br>
    Address:<input class="adress" type="text"><br>
    Password:<input class="pwd" type="password"><br>
    <button type = "button" class="button">Register</button>
   </form>
  </div>
 </div>
span{
  color:white;
 }
 body{
  font-family: sans-serif;
 }
 .main_box{
  width: 100%;
  height: 910px;
  background-color: red;
  background-image: linear-gradient(#e66465, #000000);
 }
 .title{
  font-size: 5em;
  color: white;
  width:100%;
  height: 100px;
  text-align: center;
 }
 .box{
  width: 1050px;
  height: 310px;
  margin: 150px auto 50px auto;
  padding-left: 360px;
 }
 input{
  height: 40px;
  width: 200px;
  border-radius: 20px;
  border: solid 1px #B5B5B5;
  margin: 10px;
  font-size: 1.2em;
 }
 form{
  color:white;
  font-size:1.2em;
  float: left;
  margin-left: 50px;
 }
 
 .button{
  width: 280px;
  height: 40px;
  background-color: #9781FD;
  border-radius: 25px;
  color:white;
  font-size: 1.3em;
  font-weight: 700;
  margin-top: 10px;
  
 }
 .img{
  width:310px;
  height: 310px;
  float: left;
 }

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Getting started with the basic usage of jquery validate.js form validation
  • Implementing Form Validation Based on Bootstrap+jQuery.validate
  • jQuery form validation using plugin formValidator
  • Jquery practice form validation implementation code
  • Using jQuery to implement form validation
  • Implementing form validation based on Jquery
  • jQuery form validation plugin formValidation implements personalized error prompts
  • jQuery implements form validation and prevents illegal submission
  • jQuery implements form validation example for user registration
  • Jquery plugin easyUi form validation submission (sample code)

<<:  HTML Web Page List Tags Learning Tutorial

>>:  Implementation of static website layout in docker container

Recommend

Take you to understand MySQL character set settings in 5 minutes

Table of contents 1. Content Overview 2. Concepts...

Detailed explanation of the this pointing problem in JavaScript

Summarize Global environment ➡️ window Normal fun...

BUG of odd width and height in IE6

As shown in the figure: But when viewed under IE6...

How to install and modify the initial password of mysql5.7.18 under Centos7.3

This article shares with you the installation of ...

How to load Flash in HTML (2 implementation methods)

First method : CSS code: Copy code The code is as ...

Application examples of WeChat applet virtual list

Table of contents Preface What is a virtual list?...

MySQL graphical management tool Navicat installation steps

Table of contents Preface 1. Arrange the installa...

Introduction and use of Javascript generator

What is a generator? A generator is some code tha...

Ubuntu 20.04 sets a static IP address (including different versions)

Because Ubuntu 20.04 manages the network through ...

How to modify the "Browse" button of the html form to upload files

Copy code The code is as follows: <!DOCTYPE HT...

nuxt.js multiple environment variable configuration

Table of contents 1. Introduction 2. Scenario 3. ...

Example of building a redis-sentinel cluster based on docker

1. Overview Redis Cluster enables high availabili...

Use js in html to get the local system time

Copy code The code is as follows: <div id=&quo...

How to use Docker to build a tomcat cluster using nginx (with pictures and text)

First, create a tomcat folder. To facilitate the ...

Summary of the differences between MySQL storage engines MyISAM and InnoDB

1. Changes in MySQL's default storage engine ...