溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

jQuery實(shí)現(xiàn)一個(gè)簡(jiǎn)單的驗(yàn)證碼功能

發(fā)布時(shí)間:2020-09-25 02:38:27 來(lái)源:腳本之家 閱讀:137 作者:wal1314520 欄目:web開(kāi)發(fā)

在學(xué)習(xí)jQuery過(guò)程中,寫(xiě)的一個(gè)簡(jiǎn)單的驗(yàn)證碼的小例子,記載下來(lái),方便以后借鑒補(bǔ)充,源碼如下:

<!DOCTYPE html> 
<html> 
<head> 
  <title></title> 
  <style type="text/css"> 
  div{ 
    background-color:blue; 
    width:200px; 
    height:100px; 
    font-size:35px; 
  } 
  </style> 
  <script src="../jquery-1.8.0.js" type="text/javascript"></script> 
  <script type="text/javascript"> 
    $(document).ready(function() { 
       //我寫(xiě)的驗(yàn)證碼 
      //驗(yàn)證碼 
      var code; 
      function createCode(){ 
        code = '';//首先默認(rèn)code為空字符串 
        var codeLength = 4;//設(shè)置長(zhǎng)度,這里看需求,我這里設(shè)置了4 
        var codeV = $("div"); 
        //設(shè)置隨機(jī)字符 
        var random = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R', 'S','T','U','V','W','X','Y','Z'); 
        for(var i = 0; i < codeLength; i++){ //循環(huán)codeLength 我設(shè)置的4就是循環(huán)4次   
           var index = Math.floor(Math.random()*36); //設(shè)置隨機(jī)數(shù)范圍,這設(shè)置為0 ~ 36  
           code += random[index]; //字符串拼接 將每次隨機(jī)的字符 進(jìn)行拼接 
      } 
        codeV.text(code);//將拼接好的字符串賦值給展示的Value 
      } 
      //頁(yè)面開(kāi)始加載驗(yàn)證碼 
      createCode(); 
      //驗(yàn)證碼Div加載點(diǎn)擊事件 
      $("div").bind('click',function() { 
          createCode(); 
        }); 
      //下面就是判斷是否==的代碼,無(wú)需解釋 
      $("#b1").bind('click',function() { 
         var oValue = $("#in1").val().toUpperCase(); 
         $("#l1").html(""); 
        if(oValue ==""){ 
          $("#l1").html("<font color='red'>請(qǐng)輸入驗(yàn)證碼</font>"); 
        }else if(oValue != code){ 
          $("#l1").html("<font color='red'>驗(yàn)證碼不正確,請(qǐng)重新輸入</font>"); 
          oValue = ""; 
          createCode(); 
        }else{ 
          $("#l1").html("<font color='blue'>驗(yàn)證碼正確</font>"); 
        } 
      });  
    }); 
  </script> 
</head> 
<body> 
<center> 
<label >請(qǐng)輸入驗(yàn)證碼:</label><input type="text" id="in1" value="" placeholder="請(qǐng)輸入驗(yàn)證碼"> 
<button id="b1">點(diǎn)擊驗(yàn)證</button> 
  <div></div><label id="l1"></label> 
</center> 
</body> 
</html> 

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI