×

JS生成随机码:JavaScript实现随机码的生成与校验

作者:Terry2021.07.19来源:Web前端之家浏览:4037评论:0
关键词:js

JS生成随机码:JavaScript实现随机码的生成与校验。

由于获取事件源有两种写法,所以在此处都附上:

这个是直接用var去定义的。

<!DOCTYPE html>
<html>
<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.获取对应的标签
            var codeDiv = document.getElementById("code");
            var newCodeInput = document.getElementById("newCode");
            var validate = document.getElementById("validate");

            // 加载页面获取对应验证码
            creatCode()

            // 1.获取min到max之间的整数 1~100
            function random(max,min){
                return Math.floor(Math.random()*(max-min)+min);
            }

            function creatCode(){
                code = "";
                // 设置长度
                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++){
                    // 设置随机范围36范围
                    var index = random(0,36);
                    code += randomCode[index];
                }
                codeDiv.innerHTML = code;
            }
            // 验证按钮校验
            validate.onclick = function (){
                // 获取输入用户的验证码
                var newCode = newCodeInput.value.toUpperCase();
                if (newCode === code){
                    // 验证成功  跳转对应的网址
                    window.location.href = "http://www.jiangweishan.com";
                }else {
                    // 验证失败
                    alert("验证失败,请重新输入")
                    // 输入框置空
                    newCodeInput.value = "";
                    // 重新获取验证码
                    creatCode();
                }
            }
        }
    </script>
</body>
</html>

这个是用function定义变量的:

<!DOCTYPE html>
<html>
<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.获取对应的标签(获取事件源)
            function $(id){
                return typeof id === "string"?document.getElementById(id):null;
            }

            // 加载页面获取对应验证码
            creatCode()

            // 1.获取min到max之间的整数 1~100
            function random(max,min){
                return Math.floor(Math.random()*(max-min)+min);
            }

            function creatCode(){
                code = "";
                // 设置长度
                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++){
                    // 设置随机范围36范围
                    var index = random(0,36);
                    code += randomCode[index];
                }
                $("code").innerHTML = code;
            }
            // 验证按钮校验
            $("validate").onclick = function (){
                // 获取输入用户的验证码
                var newCode = $("newCode").value.toUpperCase();
                if (newCode === code){
                    // 验证成功  跳转对应的网址
                    window.location.href = "http://www.baidu.com";
                }else {
                    // 验证失败
                    alert("验证失败,请重新输入")
                    // 输入框置空
                    $("newCode").value = "";
                    // 重新获取验证码
                    creatCode();
                }
            }
        }
    </script>
</body>
</html>

试下吧。

您的支持是我们创作的动力!
温馨提示:本文作者系Terry ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://www.jiangweishan.com/article/js20210719adf.html

网友评论文明上网理性发言 已有0人参与

发表评论: