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

编程学习 2021-07-04 14:07www.dzhlxh.cn编程入门
这篇文章主要为大家详细介绍了JavaScript实现随机码的生成与校验,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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范围
                    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>

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

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

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

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

Copyright © 2016-2025 www.dzhlxh.cn 金源码 版权所有 Power by

网站模板下载|网络推广|微博营销|seo优化|视频营销|网络营销|微信营销|网站建设|织梦模板|小程序模板