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

编辑: admin 分类: javascript 发布时间: 2022-03-11 来源:互联网

JavaScript之随机码的生成与校验,供大家参考,具体内容如下

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

这个是直接用var去定义的

<!DOCTYPE html>
<html lang="en">
<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.baidu.com";
                }else {
                    // 验证失败
                    alert("验证失败,请重新输入")
                    // 输入框置空
                    newCodeInput.value = "";
                    // 重新获取验证码
                    creatCode();
                }
            }
        }
    </script>
</body>
</html>

这个是用function定义变量的:

<!DOCTYPE html>
<html lang="en">
<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范围
          【来源:美国cn2服务器 转载请说明出处】          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>

两种方式所实现效果一样。附上效果图:

当输入错误的数据进行验证时,会提示:

当输入正确的数据进行验证时,点击验证,如果验证成功,会跳转指定路径。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持海外IDC网。