溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何用JavaScript代碼實現(xiàn)驗證碼功能

發(fā)布時間:2022-10-21 16:42:34 來源:億速云 閱讀:153 作者:iii 欄目:編程語言

這篇文章主要介紹“如何用JavaScript代碼實現(xiàn)驗證碼功能”的相關(guān)知識,小編通過實際案例向大家展示操作過程,操作方法簡單快捷,實用性強,希望這篇“如何用JavaScript代碼實現(xiàn)驗證碼功能”文章能幫助大家解決問題。

效果如下:

如何用JavaScript代碼實現(xiàn)驗證碼功能

我們需要做到的有兩點:

1、實現(xiàn)驗證碼的隨機產(chǎn)生,使其在頁面刷新和點擊更換時能夠生成
2、實現(xiàn)輸入字符串和驗證碼的比較

第一點呢,我們需要用到for循環(huán)Math.round(Math.random()*n),使得在每一次循環(huán)中可以產(chǎn)生隨機數(shù)字

第二點呢,我們只需要通過input.value獲得用戶輸入的字符串,然后將其與之前隨機產(chǎn)生的字符串進行比較即可(使用===)

其他的細(xì)節(jié)可以去代碼中查看哦

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container{
            width: 400px;
            height: 100px;
            margin:100px auto;
            background-color: hsla(180, 73%, 78%, 0.199);
            border-radius: 20px;
            text-align: center;
            padding: 20px;
        }
        #check{
            display: inline-block;
            width: 100px;
            height: 30px;
            text-align: center;
            background-color: rgba(128, 128, 128, 0.158);
            color:blue;
            font-size:26px;
            font-style: italic;
            letter-spacing: 2px;
            font-family:Arial, Helvetica, sans-serif;
            margin-bottom: 10px;
        }
        .ma{
            margin-bottom: 12px;
        }

    </style>
</head>

<body>
    <div class="container">
        <div>
            <span id="check">adf34y</span>
            <a href="#" rel="external nofollow"  id="checka">看不清換一張</a>
        </div>
        <div class="ma">
            <label for="zheng">驗證碼</label>
            <input type="text" id="zheng">
        </div>
        <button id="btn">確定</button>
    </div>

    <script>
        let sum=[0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f'];
        var check=document.getElementById("check");
        var code;
        
        function fun(){
            let str="";
            for(let i=0;i<6;i++){
                let pos=Math.round(Math.random()*15);//注意這個寫法,取隨機數(shù)
                str+=sum[pos];
            }
            check.innerHTML=str;
            return str;
        }

        window.onload=function(){
            code=fun();
        }

        let checka=document.getElementById("checka");
        checka.onclick=function(){
            code=fun();
        }
        
        let btn=document.getElementById("btn");
        btn.onclick=function(){
            let t=document.getElementById("zheng").value;
            console.log(t)
            if(t==code){
                alert("正確");
                code=fun();
                document.getElementById("zheng").value="";
            }
            else{
                alert("錯誤");
                document.getElementById("zheng").value="";
            }
        }
        

    </script>
</body>

</html>

關(guān)于“如何用JavaScript代碼實現(xiàn)驗證碼功能”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識,可以關(guān)注億速云行業(yè)資訊頻道,小編每天都會為大家更新不同的知識點。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI