亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

原生js實(shí)現(xiàn)驗(yàn)證碼功能

 更新時(shí)間:2017年03月16日 11:17:52   作者:18301695170  
本文主要介紹了原生js實(shí)現(xiàn)驗(yàn)證碼功能的實(shí)例,具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧

效果圖:

代碼如下:

<!doctype html>
<html lang="en">
 <head>
 <meta charset="UTF-8">
 <title>js驗(yàn)證碼</title>
 <style type="text/css">
 #code{
 width:80px;
 height:30px;
 font-size:20px;
 font-family:Arial; 
 font-style:italic; 
 font-weight:bold; 
 border:0; 
 letter-spacing:2px; 
 color:blue; 
 }
 </style>
 </head>
 <body>
<div> 
 <input type = "text" id = "input"/> 
 <input type = "button" id="code" /> 
 <input type = "button" value = "驗(yàn)證" id="check"/> 
</div> 
<script type="text/javascript">
window.onload=function(){
 var code=document.getElementById("code");
 function change(){
 // 驗(yàn)證碼組成庫(kù)
 var arrays=new Array( 
 '1','2','3','4','5','6','7','8','9','0', 
 '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', 
 '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'        
       ); 
  // 重新初始化驗(yàn)證碼
  codes ='';
  // 隨機(jī)從數(shù)組中獲取四個(gè)元素組成驗(yàn)證碼
  for(var i = 0; i<4; i++){
   // 隨機(jī)獲取一個(gè)數(shù)組的下標(biāo)
   var r = parseInt(Math.random()*arrays.length);
   codes += arrays[r];
  }
 // 驗(yàn)證碼添加到input里
  code.value = codes;
 }
  change();//加載顯示在頁(yè)面上
  code.onclick = change;//單擊更換驗(yàn)證碼
//單擊驗(yàn)證
 var check=document.getElementById("check");
 var input=document.getElementById("input");
 check.onclick=function(){
   var inputCode = input.value.toUpperCase(); //取得輸入的驗(yàn)證碼并轉(zhuǎn)化為大寫(xiě)   
  if(inputCode.length==0) { //若輸入的驗(yàn)證碼長(zhǎng)度為0
   alert("請(qǐng)輸入驗(yàn)證碼!"); //則彈出請(qǐng)輸入驗(yàn)證碼
  } 
  else if(inputCode!=codes.toUpperCase()) { //若輸入的驗(yàn)證碼與產(chǎn)生的驗(yàn)證碼不一致時(shí)
   alert("驗(yàn)證碼輸入錯(cuò)誤!請(qǐng)重新輸入"); //則彈出驗(yàn)證碼輸入錯(cuò)誤
   change();//刷新驗(yàn)證碼
   input.value="";//清空文本框
  }    
  else{ //輸入正確時(shí)
   alert("輸入正確"); //彈出輸入正確
  } 
  }
}
</script>
 </body>
</html>

以上就是本文的全部?jī)?nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助,同時(shí)也希望多多支持腳本之家!

相關(guān)文章

最新評(píng)論