js+css=html驗證碼驗證
阿新 • • 發佈:2019-01-08
var code ; //在全域性 定義驗證碼
function createCode()
{
code = "";
var codeLength = 6;//驗證碼的長度
var checkCode = document.getElementById("checkCode");
var selectChar = 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++)
{
var charIndex = Math.floor(Math.random()*36);
code +=selectChar[charIndex];
}
if(checkCode)
{
checkCode.className="code" ;
checkCode.value = code;
checkCode.blur();
}
}
function validate () {
var inputCode = document.getElementById("validCode").value;
if(inputCode.length <=0)
{
alert("請輸入驗證碼!");
}
else if(inputCode.toUpperCase() != code )
{
alert("驗證碼輸入錯誤!");
createCode();//重新整理驗證碼
}
else
{
alert("^-^ OK");
}
}