Generation and verification of random code with JavaScript

Time:2022-5-4

JavaScript random code generation and verification, for your reference, the specific contents are as follows

Since there are two ways to obtain event sources, they are attached here:

This is directly defined by var

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    < title > random verification code verification < / 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></div>
    <input type="text" name="">
    < input type = "button" name = "" value = "verify" >
    <script type="text/javascript">
        window.onload = function (){
            var code;
            // 1. Get the corresponding label
            var codeDiv = document.getElementById("code");
            var newCodeInput = document.getElementById("newCode");
            var validate = document.getElementById("validate");

            //Load the page to obtain the corresponding verification code
            creatCode()

            // 1. Get the integer 1 ~ 100 between min and max
            function random(max,min){
                return Math.floor(Math.random()*(max-min)+min);
            }

            function creatCode(){
                code = "";
                //Set length
                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++){
                    //Set random range 36
                    var index = random(0,36);
                    code += randomCode[index];
                }
                codeDiv.innerHTML = code;
            }
            //Verification button verification
            validate.onclick = function (){
                //Get the verification code of the input user
                var newCode = newCodeInput.value.toUpperCase();
                if (newCode === code){
                    //Verify successful jump to the corresponding URL
                    window.location.href = "http://www.baidu.com";
                }else {
                    //Validation failed
                    Alert ("verification failed, please re-enter")
                    //The input box is empty
                    newCodeInput.value = "";
                    //Retrieve verification code
                    creatCode();
                }
            }
        }
    </script>
</body>
</html>

This variable is defined by function:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    < title > random verification code verification < / 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></div>
    <input type="text" name="">
    < input type = "button" name = "" value = "verify" >
    <script type="text/javascript">
        window.onload = function (){
            var code;
            // 1. Get the corresponding tag (get event source)
            function $(id){
                return typeof id === "string"?document.getElementById(id):null;
            }

            //Load the page to obtain the corresponding verification code
            creatCode()

            // 1. Get the integer 1 ~ 100 between min and max
            function random(max,min){
                return Math.floor(Math.random()*(max-min)+min);
            }

            function creatCode(){
                code = "";
                //Set length
                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++){
                    //Set random range 36
                    var index = random(0,36);
                    code += randomCode[index];
                }
                $("code").innerHTML = code;
            }
            //Verification button verification
            $("validate").onclick = function (){
                //Get the verification code of the input user
                var newCode = $("newCode").value.toUpperCase();
                if (newCode === code){
                    //Verify successful jump to the corresponding URL
                    window.location.href = "http://www.baidu.com";
                }else {
                    //Validation failed
                    Alert ("verification failed, please re-enter")
                    //The input box is empty
                    $("newCode").value = "";
                    //Retrieve verification code
                    creatCode();
                }
            }
        }
    </script>
</body>
</html>

The two methods achieve the same effect. Attach renderings:

When you enter wrong data for verification, you will be prompted:

When entering the correct data for verification, click verify. If the verification is successful, it will jump to the specified path.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.