首頁  >  文章  >  web前端  >  jQuery Ajax顯示對號和錯號用於驗證輸入驗證碼是否正確

jQuery Ajax顯示對號和錯號用於驗證輸入驗證碼是否正確

韦小宝
韦小宝原創
2018-01-01 19:44:302545瀏覽

這篇文章主要介紹了jQuery Ajax顯示對號和錯號用於驗證輸入驗證碼是否正確的相關ajax資料,對ajax感興趣的朋友可以參考下本篇文章哦!

廢話不多說了,直接給大家貼程式碼了,具體程式碼如下圖:


#
<script src="js/j.js"></script>
<script>
$(document).ready(function(e) {
 $(&#39;#yes&#39;).hide();
 $(&#39;#no&#39;).hide();
  $(&#39;input[name=gcode]&#39;).keyup(function(){
   if($(this).val().length==4){
  $.post(&#39;gbook.php&#39;,{cc:$(&#39;input[name=gcode]&#39;).val()},function(msg){
   
   if(msg==&#39;yes&#39;){
    $(&#39;#no&#39;).hide();
    $(&#39;#yes&#39;).show();
   }else{
    $(&#39;#yes&#39;).hide();
    $("#no").show();
   }
  });
  }
 });
});
</script>


首先引入jquery


.sl-error-verifycode {
 background-image: url("images/icons.png");
 background-position: -26px 0;
 background-repeat: no-repeat;
 display: block;
 font-size: 18px;
 height: 23px;
 line-height: 20px;
 margin-left: 180px;
 margin-top: -25px;
 position: relative;
 text-align: center;
 width: 20px;
 z-index: 2;
}
.sl-correct-verifycode {
 background-image: url("images/icons.png");
 background-position: -50px 0;
 background-repeat: no-repeat;
 display: block;
 font-size: 18px;
 height: 23px;
 line-height: 20px;
 margin-left: 180px;
 margin-top: -25px;
 position: relative;
 text-align: center;
 width: 20px;
 z-index: 2;
}


#驗證碼的html 程式碼


##

<tr>
  <td height="40" align="right">验证码:</td>
  <td height="40"><span class="addred">*</span></td>
  <td height="40">
  <input type="text" name="gcode" id="textfield7" class="txtlist">
<span id="yes" class="sl-correct-verifycode"></span><span id="no" class="sl-error-verifycode"></span>
  <img style="float:right; margin-top:-25px; margin-right:300px;" align="middle" src="inc/cc.php" onClick="this.src=&#39;inc/cc.php?&#39;+new Date" title="点击换一张图片" width="100px"></td>
 </tr>


#gbook.php


<?php
session_start();
if(isset($_POST[&#39;cc&#39;])){
 $cc = strtolower($_POST[&#39;cc&#39;]);
 $ss = strtolower($_SESSION[&#39;code&#39;]);
 if($cc==$ss){
   echo "yes";
 }else{
   echo "no"; 
 }
}


#注意圖片一定要引入

以上所述是小編給大家介紹的jQuery Ajax顯示對號和錯號用來驗證輸入驗證碼是否正確,希望對大家有幫助! !

相關推薦:

實例詳解快速取得Ajax通訊物件

原生ajax瀑布流demo實例分享

實例講解H5移動開發Ajax上傳多張Base64格式圖片到伺服器#

以上是jQuery Ajax顯示對號和錯號用於驗證輸入驗證碼是否正確的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn