javascript實現登錄驗證碼


1.js

var code="" ; //在全局 定義驗證碼
function createCode(){ 
code = "";
var codeLength = 6;//驗證碼的長度
var checkCode = document.getElementById("checkCode");
checkCode.value = "";
var selectChar = new Array(0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','J','K','L','M','N','P','Q','R','S','T','U','V','W','X','Y','Z');

for(var i=0;i<codeLength;i++) {
   var charIndex = Math.floor(Math.random()*32);
   code +=selectChar[charIndex];
}
if(code.length != codeLength){
   createCode();
}
document.getElementById("checkCode").innerHTML = code;
}

function validate () {
var inputCode = document.getElementById("checkNum").value.toUpperCase();

if(inputCode.length <=0) {
   alert("請輸入驗證碼!");
   return false;
}
else if(inputCode != code ){
   alert("驗證碼輸入錯誤!");
   createCode();
   return false;
}
else {
   alert("驗證碼通過!");
   return true;
}

}

2.html

 <body onload="createCode();">
<input type="text" value="" id="checkNum" />
<a id="checkCode" onclick="createCode()"></a>
<input type="button" class="btnCheck" id="gotoCheck"  value="驗證"  onclick="validate();" />

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM