JavaScript實現(xiàn)隨機碼的生成與校驗
JavaScript之隨機碼的生成與校驗,供大家參考,具體內(nèi)容如下
由于獲取事件源有兩種寫法,所以在此處都附上:
這個是直接用var去定義的
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>隨機驗證碼校驗</title> <style type="text/css"> #code{ width: 100px; height: 100px; background-color: #ddd; padding: 10px; line-height: 100px; text-align: center; font-size: 20px; color: red; /*font-weight: bold;*/ } </style> </head> <body> <div id="code"></div> <input type="text" name="" id="newCode"> <input type="button" name="" id="validate" value="驗證"> <script type="text/javascript"> window.onload = function (){ var code; // 1.獲取對應(yīng)的標(biāo)簽 var codeDiv = document.getElementById("code"); var newCodeInput = document.getElementById("newCode"); var validate = document.getElementById("validate"); // 加載頁面獲取對應(yīng)驗證碼 creatCode() // 1.獲取min到max之間的整數(shù) 1~100 function random(max,min){ return Math.floor(Math.random()*(max-min)+min); } function creatCode(){ code = ""; // 設(shè)置長度 var codeLenth = 4; var randomCode =[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<codeLenth;i++){ // 設(shè)置隨機范圍36范圍 var index = random(0,36); code += randomCode[index]; } codeDiv.innerHTML = code; } // 驗證按鈕校驗 validate.onclick = function (){ // 獲取輸入用戶的驗證碼 var newCode = newCodeInput.value.toUpperCase(); if (newCode === code){ // 驗證成功 跳轉(zhuǎn)對應(yīng)的網(wǎng)址 window.location.; }else { // 驗證失敗 alert("驗證失敗,請重新輸入") // 輸入框置空 newCodeInput.value = ""; // 重新獲取驗證碼 creatCode(); } } } </script> </body> </html>
這個是用function定義變量的:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>隨機驗證碼校驗</title> <style type="text/css"> #code{ width: 100px; height: 100px; background-color: #ddd; padding: 10px; line-height: 100px; text-align: center; font-size: 20px; color: red; /*font-weight: bold;*/ } </style> </head> <body> <div id="code"></div> <input type="text" name="" id="newCode"> <input type="button" name="" id="validate" value="驗證"> <script type="text/javascript"> window.onload = function (){ var code; // 1.獲取對應(yīng)的標(biāo)簽(獲取事件源) function $(id){ return typeof id === "string"?document.getElementById(id):null; } // 加載頁面獲取對應(yīng)驗證碼 creatCode() // 1.獲取min到max之間的整數(shù) 1~100 function random(max,min){ return Math.floor(Math.random()*(max-min)+min); } function creatCode(){ code = ""; // 設(shè)置長度 var codeLenth = 4; var randomCode =[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<codeLenth;i++){ // 設(shè)置隨機范圍36范圍 var index = random(0,36); code += randomCode[index]; } $("code").innerHTML = code; } // 驗證按鈕校驗 $("validate").onclick = function (){ // 獲取輸入用戶的驗證碼 var newCode = $("newCode").value.toUpperCase(); if (newCode === code){ // 驗證成功 跳轉(zhuǎn)對應(yīng)的網(wǎng)址 window.location.; }else { // 驗證失敗 alert("驗證失敗,請重新輸入") // 輸入框置空 $("newCode").value = ""; // 重新獲取驗證碼 creatCode(); } } } </script> </body> </html>
兩種方式所實現(xiàn)效果一樣。附上效果圖:
當(dāng)輸入錯誤的數(shù)據(jù)進行驗證時,會提示:
當(dāng)輸入正確的數(shù)據(jù)進行驗證時,點擊驗證,如果驗證成功,會跳轉(zhuǎn)指定路徑。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
利用Javascript裁剪圖片并存儲的簡單實現(xiàn)
裁剪圖片對我們來說是再熟悉不過的了,最近工作中就又遇到了這個需求,所以想著干脆整理下來,方法大家和自己在需要的時候參考學(xué)習(xí),所以這篇文章主要介紹了利用Javascript裁剪圖片并存儲的簡單實現(xiàn),后端PHP處理我用的是THINKPHP框架,需要的朋友可以參考下。2017-03-03DOM節(jié)點刪除函數(shù)removeChild()用法實例
這篇文章主要介紹了DOM節(jié)點刪除函數(shù)removeChild()用法,實例分析了removeChild()函數(shù)實現(xiàn)結(jié)點刪除的技巧,需要的朋友可以參考下2015-01-01JS解析json數(shù)據(jù)并將json字符串轉(zhuǎn)化為數(shù)組的實現(xiàn)方法
json數(shù)據(jù)在ajax實現(xiàn)異步交互時起到了很重要的作用,他可以返回請求的數(shù)據(jù),然后利用客戶端的js進行解析,這一點體現(xiàn)出js的強大,本文介紹JS解析json數(shù)據(jù)并將json字符串轉(zhuǎn)化為數(shù)組的實現(xiàn)方法,需要了解的朋友可以參考下2012-12-12js實現(xiàn)帶農(nóng)歷和八字等信息的日歷特效
本文主要介紹了js實現(xiàn)帶農(nóng)歷和八字等信息的日歷特效,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考2016-05-05echarts報錯Cannot?read?properties?of?null?(reading?‘getA
最近在開發(fā)Echarts忽然遇到了個問題,這篇文章主要給大家介紹了關(guān)于echarts報錯Cannot?read?properties?of?null?(reading?‘getAttribute‘)的解決方法,需要的朋友可以參考下2023-01-01JS實現(xiàn)把鼠標(biāo)放到鏈接上出現(xiàn)滾動文字的方法
這篇文章主要介紹了JS實現(xiàn)把鼠標(biāo)放到鏈接上出現(xiàn)滾動文字的方法,涉及JavaScript響應(yīng)鼠標(biāo)事件動態(tài)操作頁面元素的相關(guān)技巧,需要的朋友可以參考下2016-04-04