廢話不多說,實現js登入驗證碼的功能需要下面兩步,具體實現過程如下所示:
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();" />
以上所述是小編給大家介紹的JavaScript登入驗證碼的實現,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!