js案例——实现验证码功能

版权声明:转载声明原作者及链接 https://blog.csdn.net/ICY___/article/details/86517691

今天的内容是用js的基本语法实现网页中常见的验证码功能。

大体思想:核心语法为math中随机数的使用 利用随机数字来实现随机的数字 字符 汉字  ,利用字符串的拼接来实现随机字体大小、旋转角度和各种颜色,利用css样式来禁止从网页上复制和粘贴,最后用弹窗来实现验证结果提示。

实现结果:

具体代码及注释:

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

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>验证码</title>
    <style>
        /* body{
        margin: 0%;
        padding: 0%;
    } */
    /* html,body{
        -webkit-user-select: none;
        -moz-user-select: none;
        -ms-user-select: none;} */
/* 分别是谷歌,火狐,IE浏览器的对应禁止选中 */
/* 通过css样式来实现禁止复制验证码  建议使用*/
    .block{
        width: 100px;
        height: 40px;
        border: 1px solid grey;
        float: left;
        background-image: url(img/1.png);
        background-size: 100%;
        overflow: hidden;
    }
    #look{
        float: left;
        color: black;
        font-size: 12px;
        position: relative;
        cursor: pointer;
        top: 28px;
        left: 5px;    
    }
    #count{
        float: left
    }
    .num{
        display: inline-block;
        line-height: 40px;
        width: 16.5px;
        text-align: center;
    }
    </style>
</head>

<body>
    <div>
        <div class="block"></div><!--验证码显示区域  需要注意此区域不能复制!-->
        <span id="look">看不清...</span><!--防止无法辨识的情况,点击随机生成验证码-->
        <div style="clear: both;"><!--用于清除浮动-->
            <input type="text" id="count"><!--用户输入栏,将验证码输入在此,且不区分大小写-->
            <button id="btn">验证</button><!--点击是比较值,验证成功提示成功,失败提示失败且再次随机生成验证码-->
        </div>
    </div>
    <script>
        var block = document.getElementsByClassName('block')[0];
       document.onselectstart=function (){
             return false;//禁止选择开始
            //   建议还是用样式来禁用复制   
        }
        var res;
        showdata();

        function showdata() {
        // 在实现验证码的相应功能时,要有先后顺序,先简单在复杂,要有递进关系
        // 个人实现方案:1.先实现数字验证码的随机显示,之后加上点击“看不清”重新随机生成验证码,最后加入验证功能
        // 2.在实现了数字验证码的验证后,优化代码,考虑用户输入错误的结果,考虑验证码的显示效果
        // 3.最后加入字符和汉字,使得验证码可以随机产生不同类型的样式
            res = ''; //比较值的数组
            var ele = ''; //拼接值的数组  重新调用时,将值附空 
            for (var i = 0; i < 6; i++) {//产生6位验证码
                if (Math.random() > 0.33) {//产生数字验证码的概率为33%
                    var number = Math.floor(Math.random() * 10);//Math.random 为0~1且不为1的随机数,*10后向下取整为0~9的随机数
                    res += number;
                    ele += "<span class='num' style='transform: rotate(" + Math.floor((Math.random() - 0.5) * 80) +"deg); font-size:" + (Math.floor(Math.random() * 8) + 12) + "px; color:rgb(" + Math.floor(Math.random() *256) + "," + Math.floor(Math.random() * 256) + "," + Math.floor(Math.random() * 256) +"); '>" + number + "</span>";
                    // 字符串拼接 需要多练习
                } else if (Math.random() > 0.33 && Math.random() < 0.8) {//关于调用的Math.random() 由于是每次调用都会重新产生新的随机数,所以它的值不为固定值。
                // 也就是说,上行代码可以换做  Math.random()<0.46   由于它不是固定值,所以它只表示概率!!!
                    var number = (97 + Math.floor(Math.random() * 26));
                    var str = ""; //需要转化为字符
                    if (Math.random > 0.5) {
                        str = String.fromCharCode(number).toUpperCase(); //小写
                    } else {
                        str = String.fromCharCode(number).toUpperCase(); //大写
                    }
                    res += str;
                    ele += "<span class='num' style='transform: rotate(" + Math.floor((Math.random() - 0.5) * 80) +
                        "deg); font-size:" + (Math.floor(Math.random() * 8) + 12) + "px; color:rgb(" + Math.floor(Math.random() *
                            256) + "," + Math.floor(Math.random() * 256) + "," + Math.floor(Math.random() * 256) +
                        "); '>" + str + "</span>";
                } else {
                    var str_new = "昨夜雨疏风骤浓睡不消残酒试问卷帘人却道海棠依旧知否知否应是绿肥红瘦";//可以找一段自己喜欢的文字,用来充当文字库
                    var index=Math.floor(Math.random()*str_new.length);
                    ele += "<span class='num' style='transform: rotate(" + Math.floor((Math.random() - 0.5) * 80) + "deg); font-size:" + (Math.floor(Math.random() * 8) + 12) + "px; color:rgb(" + Math.floor(Math.random() * 256) + "," + Math.floor(Math.random() * 256) + "," + Math.floor(Math.random() * 256) + "); '>" + str_new.charAt(index) + "</span>";
                    res+=str_new.charAt(index);
                }


            }
            block.innerHTML = ele;//将拼接的内容赋给block 实现验证码的显示
        }
        //  看不清
        var look = document.getElementById('look');
        look.onclick = function () {
            showdata();//重新调用即可实现重新生成验证码 
        }
        //  验证按钮
        var btn = document.getElementById('btn');
        var count = document.getElementById('count');
        btn.onclick = function () {
            if (count.value.toLowerCase() == res.toLowerCase()) {
                alert('验证成功');
                // 弹窗
            } else {
                alert('验证失败');
                showdata();
                // 重新调用,实现实现重新生成验证码 
                count.value = '';
                //将之前的文本框内容清空
            }
        }
    </script>


</body>

</html>

到此,这个比较简单却功能丰富的验证码案例结束了。

猜你喜欢

转载自blog.csdn.net/ICY___/article/details/86517691