欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JavaScript實(shí)現(xiàn)隨機(jī)碼的生成與校驗(yàn)

 更新時(shí)間:2021年04月29日 11:43:17   作者:知識(shí)進(jìn)腦的肖老千啊  
這篇文章主要為大家詳細(xì)介紹了JavaScript實(shí)現(xiàn)隨機(jī)碼的生成與校驗(yàn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

JavaScript之隨機(jī)碼的生成與校驗(yàn),供大家參考,具體內(nèi)容如下

由于獲取事件源有兩種寫法,所以在此處都附上:

這個(gè)是直接用var去定義的

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>隨機(jī)驗(yàn)證碼校驗(yàn)</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="驗(yàn)證">
    <script type="text/javascript">
        window.onload = function (){
            var code;
            // 1.獲取對(duì)應(yīng)的標(biāo)簽
            var codeDiv = document.getElementById("code");
            var newCodeInput = document.getElementById("newCode");
            var validate = document.getElementById("validate");

            // 加載頁(yè)面獲取對(duì)應(yīng)驗(yàn)證碼
            creatCode()

            // 1.獲取min到max之間的整數(shù) 1~100
            function random(max,min){
                return Math.floor(Math.random()*(max-min)+min);
            }

            function creatCode(){
                code = "";
                // 設(shè)置長(zhǎng)度
                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è)置隨機(jī)范圍36范圍
                    var index = random(0,36);
                    code += randomCode[index];
                }
                codeDiv.innerHTML = code;
            }
            // 驗(yàn)證按鈕校驗(yàn)
            validate.onclick = function (){
                // 獲取輸入用戶的驗(yàn)證碼
                var newCode = newCodeInput.value.toUpperCase();
                if (newCode === code){
                    // 驗(yàn)證成功  跳轉(zhuǎn)對(duì)應(yīng)的網(wǎng)址
                    window.location.;
                }else {
                    // 驗(yàn)證失敗
                    alert("驗(yàn)證失敗,請(qǐng)重新輸入")
                    // 輸入框置空
                    newCodeInput.value = "";
                    // 重新獲取驗(yàn)證碼
                    creatCode();
                }
            }
        }
    </script>
</body>
</html>

這個(gè)是用function定義變量的:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>隨機(jī)驗(yàn)證碼校驗(yàn)</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="驗(yàn)證">
    <script type="text/javascript">
        window.onload = function (){
            var code;
            // 1.獲取對(duì)應(yīng)的標(biāo)簽(獲取事件源)
            function $(id){
                return typeof id === "string"?document.getElementById(id):null;
            }

            // 加載頁(yè)面獲取對(duì)應(yīng)驗(yàn)證碼
            creatCode()

            // 1.獲取min到max之間的整數(shù) 1~100
            function random(max,min){
                return Math.floor(Math.random()*(max-min)+min);
            }

            function creatCode(){
                code = "";
                // 設(shè)置長(zhǎng)度
                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è)置隨機(jī)范圍36范圍
                    var index = random(0,36);
                    code += randomCode[index];
                }
                $("code").innerHTML = code;
            }
            // 驗(yàn)證按鈕校驗(yàn)
            $("validate").onclick = function (){
                // 獲取輸入用戶的驗(yàn)證碼
                var newCode = $("newCode").value.toUpperCase();
                if (newCode === code){
                    // 驗(yàn)證成功  跳轉(zhuǎn)對(duì)應(yīng)的網(wǎng)址
                    window.location.;
                }else {
                    // 驗(yàn)證失敗
                    alert("驗(yàn)證失敗,請(qǐng)重新輸入")
                    // 輸入框置空
                    $("newCode").value = "";
                    // 重新獲取驗(yàn)證碼
                    creatCode();
                }
            }
        }
    </script>
</body>
</html>

兩種方式所實(shí)現(xiàn)效果一樣。附上效果圖:

當(dāng)輸入錯(cuò)誤的數(shù)據(jù)進(jìn)行驗(yàn)證時(shí),會(huì)提示:

當(dāng)輸入正確的數(shù)據(jù)進(jìn)行驗(yàn)證時(shí),點(diǎn)擊驗(yàn)證,如果驗(yàn)證成功,會(huì)跳轉(zhuǎn)指定路徑。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論