×

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

作者:Web前端之家2021.07.19来源:Web前端之家浏览:137评论: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>

试下吧。

温馨提示:本文作者系Web前端之家 ,经Web前端之家编辑修改或补充,转载请注明出处和本文链接:
https://jiangweishan.com/article/js20210719adf.html

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

发表评论:

最新留言

  • 大吨位油缸

    谷歌浏览器使用起来很快,没有乱七八糟的插件。www.sdlyxt.com...

  • Web前端之家

    可以加Q群...

  • 访客

    JS点击锚点实现平滑滚动能写一个备注呗,有点看不懂,谢谢...

  • alice

    试用了下,vee-validate挺好用的!...

  • 访客

    封装思路跟原生javascript基本差不多。...

  • qianduan

    这个跟H5做的很像,看上去用react实现起来要简单些。...

  • qianduan

    YYDS的文章,收藏了。...

  • 访客

    红红火火恍恍惚惚...

首页|JavaScript|HTML|HTML4|HTML5|CSS3|开发工具|性能优化|移动开发|前端教程|性能优化|开发工具|酷站欣赏|UI设计|前端教程

Copyright © 2021 Web前端之家(www.jiangweishan.com) 版权所有 All Rights Reserved.
粤ICP备12067512号-1

Copyright Your WebSite.Some Rights Reserved.

Powered By Z-BlogPHP 1.6.7 Valyria