欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

jQuery實(shí)現(xiàn)表單驗(yàn)證功能

 更新時(shí)間:2021年10月21日 14:14:01   作者:趕不上明天  
這篇文章主要為大家詳細(xì)介紹了jQuery實(shí)現(xiàn)表單驗(yàn)證功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

jQuery表單驗(yàn)證實(shí)例 / 包含用戶名、密碼、住址、郵箱驗(yàn)證

如下圖

別忘了引入jQuery框架?。?!

話不多說(shuō)直接先上jQuery部分代碼:

<script type="text/javascript">
 $(document).ready(function(){
  var tip1 = "<span class='span1'>用戶名不能為空!</span>";//聲明發(fā)生錯(cuò)誤時(shí)在輸入框后面添加的span
  var tip2 = "<span class='span2'>郵箱格式錯(cuò)誤或不能為空!</span>";
  var tip3 = "<span class='span3'>地址不能為空!</span>";
  var tip4 = "<span class='span4'>密碼長(zhǎng)度不能小于五位且最多為十位 !</span>";
  var condition = /^([\.a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(\.[a-zA-Z0-9_-])+/;//聲明判定郵箱格式的條件
  $(".id").blur(function(){
   if(!$(".id").val()){//判定用戶名非空
    $(".span1").remove();
    $(".id").after(tip1);
   }
   else{
    $(".span1").remove();
   }
  });
  $(".email").blur(function(){
   
   if(!condition.test($(".email").val())){//判定郵箱格式
    $(".span2").remove();
    $(".email").after(tip2);
   }
   else{
    $(".span2").remove();
   }

  });
  $(".adress").blur(function(){
   if(!$(".adress").val()){//判定地址非空
    $(".span3").remove();
    $(".adress").after(tip3);
   }
   else{
    $(".span3").remove();
   }
  });
  $(".pwd").blur(function(){
   
   if($(".pwd").val().length < 5||$(".pwd").val().length >10){//判定密碼長(zhǎng)度不能小于5位且不能大于10位
    $(".span4").remove();
    $(".pwd").after(tip4);
   }
   else{
    $(".span4").remove();
   }
  });
  $(".button").click(function(){//符合所有條件則彈出彈窗表單驗(yàn)證通過,如果不符合則彈出彈窗提醒用戶更改
   if(!$(".id").val()||!condition.test($(".email").val())||!$(".adress").val()||$(".pwd").val().length < 5||$(".pwd").val().length >10){
    alert("注冊(cè)信息有誤,請(qǐng)更改個(gè)人信息");
   }
   else{
    alert("注冊(cè)成功");
   }
  })
 })
</script>

結(jié)構(gòu)和樣式:

<div class="main_box">
  <div class="title">
   歡迎注冊(cè)原魔
  </div>
  <div class="box">
  <img alt="插圖"  src="./img/可莉派萌.png" class="img">
   <form>
    用戶名:<input class="id" type="text" ><br>
    郵&emsp;箱:<input class="email" type="text"><br>
    地&emsp;址:<input class="adress" type="text"><br>
    密&emsp;碼:<input class="pwd" type="password"><br>
    <button type = "button" class="button">注&emsp;&emsp;&emsp;冊(cè)</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;
 }

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論