如何用js生成簡單驗證碼,並驗證是否正確的方法
1、html頁面如下
<div> <table border="0" cellspacing="5" cellpadding="5" > <tr> <td> <div id="checkCode" class="code" onclick="createCode(4)" ></div></td> <td> <span onclick="createCode(4)">看不清換一張</span></td> </tr> <tr> <td>驗證碼:</td> <td><input type="text" id="inputCode" style="float:left;" /></td> </tr> <tr> <td></td> <td><input type="button" onclick="validateCode()" value="確定" /></td> </tr> </table> </div>
2、js腳本如下
//頁面加載時,生成隨機驗證碼 window.onload=function(){ createCode(4); } //生成驗證碼的方法 function createCode(length) { var code = ""; var codeLength = parseInt(length); //驗證碼的長度 var checkCode = document.getElementById("checkCode"); ////所有候選組成驗證碼的字符,當然也可以用中文的 var codeChars = 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', '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 charNum = Math.floor(Math.random() * 62); //組合成指定字符驗證碼 code += codeChars[charNum]; } if (checkCode) { //為驗證碼區域添加樣式名 checkCode.className = "code"; //將生成驗證碼賦值到顯示區 checkCode.innerHTML = code; } } //檢查驗證碼是否正確 function validateCode() { //獲取顯示區生成的驗證碼 var checkCode = document.getElementById("checkCode").innerHTML; //獲取輸入的驗證碼 var inputCode = document.getElementById("inputCode").value; //console.log(checkCode); //console.log(inputCode); if (inputCode.length <= 0) { alert("請輸入驗證碼!"); } else if (inputCode.toUpperCase() != checkCode.toUpperCase()) { alert("驗證碼輸入有誤!"); createCode(4); } else { alert("驗證碼正確!"); } }
3、驗證碼效果圖如下:


注:createCode可以傳遞參數,決定生成驗證碼的位數
4、整體demo源碼:
<!doctype html> <html> <head> <meta charset="utf-8"> <title>js簡單驗證碼使用</title> <style> .code { font-family:Arial; font-style:italic; color:blue; font-size:30px; border:0; padding:2px 3px; letter-spacing:3px; font-weight:bolder; float:left; cursor:pointer; width:150px; height:50px; line-height:60px; text-align:center; vertical-align:middle; background-color:#D8B7E3; } span { text-decoration:none; font-size:12px; color:#288bc4; padding-left:10px; } span:hover { text-decoration:underline; cursor:pointer; } </style> </head> <script> //頁面加載時,生成隨機驗證碼 window.onload=function(){ createCode(4); } //生成驗證碼的方法 function createCode(length) { var code = ""; var codeLength = parseInt(length); //驗證碼的長度 var checkCode = document.getElementById("checkCode"); ////所有候選組成驗證碼的字符,當然也可以用中文的 var codeChars = 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', '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 charNum = Math.floor(Math.random() * 62); //組合成指定字符驗證碼 code += codeChars[charNum]; } if (checkCode) { //為驗證碼區域添加樣式名 checkCode.className = "code"; //將生成驗證碼賦值到顯示區 checkCode.innerHTML = code; } } //檢查驗證碼是否正確 function validateCode() { //獲取顯示區生成的驗證碼 var checkCode = document.getElementById("checkCode").innerHTML; //獲取輸入的驗證碼 var inputCode = document.getElementById("inputCode").value; console.log(checkCode); console.log(inputCode); if (inputCode.length <= 0) { alert("請輸入驗證碼!"); } else if (inputCode.toUpperCase() != checkCode.toUpperCase()) { alert("驗證碼輸入有誤!"); createCode(4); } else { alert("驗證碼正確!"); } } </script> <body> <div> <table border="0" cellspacing="5" cellpadding="5" > <tr> <td> <div id="checkCode" class="code" onclick="createCode(4)" ></div></td> <td> <span onclick="createCode(4)">看不清換一張</span></td> </tr> <tr> <td>驗證碼:</td> <td><input type="text" id="inputCode" style="float:left;" /></td> </tr> <tr> <td></td> <td><input type="button" onclick="validateCode()" value="確定" /></td> </tr> </table> </div> </body> </html>
