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

Should I abandon JQuery?

Table of contents Preface What to use if not jQue...

Vue implements a scroll bar style

At first, I wanted to modify the browser scroll b...

Top 10 useful and important open source tools in 2019

In Black Duck's 2017 open source survey, 77% ...

MySQL optimization: how to write high-quality SQL statements

Preface There are a lot of information and method...

Implementation example of react project from new creation to deployment

Start a new project This article mainly records t...

How to solve the 2002 error when installing MySQL database on Alibaba Cloud

The following error occurred while installing the...

Docker enables seamless calling of shell commands between container and host

As shown below: nsenter -t 1 -m -u -n -i sh -c &q...

MySQL query optimization using custom variables

Table of contents Optimizing sorting queries Avoi...

Summary of MySQL 8.0 memory-related parameters

Theoretically, the memory used by MySQL = global ...

In-depth understanding of JavaScript callback functions

Table of contents Preface Quick Review: JavaScrip...

Write a React-like framework from scratch

Recently I saw the article Build your own React o...

Native JS realizes compound motion of various motions

This article shares with you a compound motion im...

Simple example of HTML checkbox and radio style beautification

Simple example of HTML checkbox and radio style b...