JS实现自动生6位验证码

该博客详细介绍了如何使用HTML、CSS和JavaScript实现一个动态验证码功能。页面加载时自动生成6位由0-9和a-f组成的随机验证码,并显示在页面上。用户可以点击看不清换一张按钮更换新的验证码。如果用户输入的验证码与显示的不一致,点击确定按钮会弹出错误提示并清空输入框。此代码段涵盖了前端验证的基本逻辑和交互设计。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

详细代码如下:

<!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>
        .v_code {
            width: 600px;
            margin: 0 auto;
        }

        .v_code>input {
            width: 60px;
            height: 36px;
            margin-top: 10px;
        }

        .code_show {
            overflow: hidden;
        }

        .code_show span {
            display: block;
            float: left;
            margin-bottom: 10px;
        }

        .code_show a {
            display: block;
            float: left;
            margin-top: 10px;
            margin-left: 10px;
        }

        .code {
            font-style: italic;
            background-color: #f5f5f5;
            color: blue;
            font-size: 30px;
            letter-spacing: 3px;
            font-weight: bolder;
            float: left;
            cursor: pointer;
            padding: 0 5px;
            text-align: center;
        }

        #inputCode {
            width: 100px;
            height: 30px;
        }

        a {
            text-decoration: none;
            font-size: 12px;
            color: #288bc4;
            cursor: pointer;
        }

        a:hover {
            text-decoration: underline;
        }
    </style>
</head>

<body>
    <div class="v_code">
        <div class="code_show">
            <span class="code" id="checkCode"></span>
            <a id="linkbt">看不清换一张</a>
        </div>

        <div class="input_code">
            <label for="inputCode">验证码:</label>
            <input type="text" id="inputCode" />
            <span id="text_show"></span>
        </div>
        <input id="Button1" type="button" value="确定" />
    </div>
    <script>
        //6位数 0-9 a-f 随机生成,内容必须是0-9 a-f字符串
        window.onload = function () {
            var res = getCode();//当页面加载的时候调用下面获取6位字符串的方法
            document.getElementById('checkCode').innerText = res;//把它赋值给span显示

            //获取6位字符串的方法
            function getCode() {
                //定义字符数组
                var arr = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'];
                var str = '';//定义空的字符串,作为最终的结果接收它
                for (var i = 0; i < 6; i++) {
                    var num = Math.round(Math.random() * (15 - 0) + 0);//0-15的随机数
                    str += arr[num];//通过产生的随机数来获取数组中的内容
                }
                return str;
            }


            //当点击连接换一张时调用方法重新生成6位字符串赋值给span
            document.getElementById('linkbt').onclick = function () {
                document.getElementById('checkCode').innerText = getCode();
            }

            //功能2:提交时进行验证输入是否正确
            document.getElementById('Button1').onclick = function () {
                var code = document.getElementById('checkCode').innerText;
                var inputCode = document.getElementById('inputCode').value;
                if (code != inputCode) {
                    alert('您输入的验证码不正确');
                    //让输入框为空,清空输入框
                    document.getElementById('inputCode').value = '';
                }
            }
        }

    </script>
</body>

</html>

结果:初始页面,每当页面刷新一次,自动更新验证码:

当点击看不清更换一张时,更换验证码:

 当输入验证码错误,点击确定时,进行提示:

 

点击确定后自动清空验证码: 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

喵俺第一专栏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值