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

jQuery實現(xiàn)一個簡單的驗證碼功能

 更新時間:2017年06月26日 09:47:21   作者:wal1314520  
今天給大家分享一個基于jquery實現(xiàn)的簡單驗證碼功能,代碼簡單易懂,非常不錯,具有參考借鑒價值,需要的的朋友參考下吧

在學習jQuery過程中,寫的一個簡單的驗證碼的小例子,記載下來,方便以后借鑒補充,源碼如下:

<!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() { 
       //我寫的驗證碼 
      //驗證碼 
      var code; 
      function createCode(){ 
        code = '';//首先默認code為空字符串 
        var codeLength = 4;//設置長度,這里看需求,我這里設置了4 
        var codeV = $("div"); 
        //設置隨機字符 
        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 我設置的4就是循環(huán)4次   
           var index = Math.floor(Math.random()*36); //設置隨機數(shù)范圍,這設置為0 ~ 36  
           code += random[index]; //字符串拼接 將每次隨機的字符 進行拼接 
      } 
        codeV.text(code);//將拼接好的字符串賦值給展示的Value 
      } 
      //頁面開始加載驗證碼 
      createCode(); 
      //驗證碼Div加載點擊事件 
      $("div").bind('click',function() { 
          createCode(); 
        }); 
      //下面就是判斷是否==的代碼,無需解釋 
      $("#b1").bind('click',function() { 
         var oValue = $("#in1").val().toUpperCase(); 
         $("#l1").html(""); 
        if(oValue ==""){ 
          $("#l1").html("<font color='red'>請輸入驗證碼</font>"); 
        }else if(oValue != code){ 
          $("#l1").html("<font color='red'>驗證碼不正確,請重新輸入</font>"); 
          oValue = ""; 
          createCode(); 
        }else{ 
          $("#l1").html("<font color='blue'>驗證碼正確</font>"); 
        } 
      });  
    }); 
  </script> 
</head> 
<body> 
<center> 
<label >請輸入驗證碼:</label><input type="text" id="in1" value="" placeholder="請輸入驗證碼"> 
<button id="b1">點擊驗證</button> 
  <div></div><label id="l1"></label> 
</center> 
</body> 
</html> 

相關(guān)文章

最新評論