JavaScript realizes the generation and verification of random codes

JavaScript realizes the generation and verification of random codes

The generation and verification of random codes in JavaScript is for your reference. The specific contents are as follows

Since there are two ways to get the event source, both are attached here:

This is defined directly using 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 id="code"></div>
    <input type="text" name="" id="newCode">
    <input type="button" name="" id="validate" value="Validate">
    <script type="text/javascript">
        window.onload = function (){
            var code;
            // 1. Get the corresponding tag var codeDiv = document.getElementById("code");
            var newCodeInput = document.getElementById("newCode");
            var validate = document.getElementById("validate");

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

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

            function createCode(){
                code = "";
                // Set the 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 the random range to 36 range var index = random(0,36);
                    code += randomCode[index];
                }
                codeDiv.innerHTML = code;
            }
            // Validation button validation validate.onclick = function (){
                // Get the verification code entered by the user var newCode = newCodeInput.value.toUpperCase();
                if (newCode === code){
                    //Verify that the corresponding URL is successfully jumped window.location.href = "http://www.baidu.com";
                }else {
                    // Verification failed alert("Verification failed, please re-enter")
                    // Empty the input box newCodeInput.value = "";
                    // Get the verification code again creatCode();
                }
            }
        }
    </script>
</body>
</html>

This is how to define variables using 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 id="code"></div>
    <input type="text" name="" id="newCode">
    <input type="button" name="" id="validate" value="Validate">
    <script type="text/javascript">
        window.onload = function (){
            var code;
            // 1. Get the corresponding tag (get the event source)
            function $(id){
                return typeof id === "string"?document.getElementById(id):null;
            }

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

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

            function createCode(){
                code = "";
                // Set the 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 the random range to 36 range var index = random(0,36);
                    code += randomCode[index];
                }
                $("code").innerHTML = code;
            }
            // Validation button check $("validate").onclick = function () {
                // Get the verification code entered by the user var newCode = $("newCode").value.toUpperCase();
                if (newCode === code){
                    //Verify that the corresponding URL is successfully jumped window.location.href = "http://www.baidu.com";
                }else {
                    // Verification failed alert("Verification failed, please re-enter")
                    // Empty the input box$("newCode").value = "";
                    // Get the verification code again creatCode();
                }
            }
        }
    </script>
</body>
</html>

Both methods achieve the same effect. Attached is the effect picture:

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

When you enter the correct data for verification, click Verify. If the verification is successful, you will be redirected to the specified path.

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • js implements random number and letter verification code
  • JavaScript random verification code generation example code
  • js randomly generates a verification code
  • Implement a random verification code generation function based on JS
  • How to generate random verification code in JS
  • js implements random 8-digit verification code
  • JavaScript implements random five-digit verification code
  • JS implements random verification code function
  • JS implements 4-digit random verification code
  • JavaScript generates random verification code code example

<<:  Detailed explanation of the use of mysql explain (analysis index)

>>:  This article summarizes the implementation methods of 6 load balancing technologies (summary)

Recommend

Detailed explanation of the use of React.cloneElement

Table of contents The role of cloneElement Usage ...

Detailed explanation of flex layout in CSS

Flex layout is also called elastic layout. Any co...

Limit input type (multiple methods)

1. Only Chinese characters can be input and pasted...

Detailed tutorial on building a private Git server on Linux

1. Server setup The remote repository is actually...

Detailed tutorial on installing Nginx 1.16.0 under Linux

Because I have been tinkering with Linux recently...

Implementation of CSS dynamic height transition animation effect

This question originated from a message on Nugget...

CentOS 8 Installation Guide for Zabbix 4.4

Zabbix server environment platform ZABBIX version...

MySQL slow_log table cannot be modified to innodb engine detailed explanation

background Getting the slow query log from mysql....

Detailed explanation of query examples within subqueries in MySql

Where is my hometown when I look northwest? How m...

Using js to achieve waterfall effect

This article example shares the specific code of ...

Linux Centos8 Create CA Certificate Tutorial

Install Required Files Yum install openssl-* -y C...

Introduction to using the MySQL mysqladmin client

Table of contents 1. Check the status of the serv...

Add a floating prompt for the header icon in the ElementUI table

This article mainly introduces how to add floatin...

Example of how to exit the loop in Array.forEach in js

Table of contents forEach() Method How to jump ou...