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

Simple method to install mysql under linux

When searching online for methods to install MySQ...

Summary of Button's four Click response methods

Button is used quite a lot. Here I have sorted ou...

VMware15.5 installation Ubuntu20.04 graphic tutorial

1. Preparation before installation 1. Download th...

MySQL example of getting today and yesterday's 0:00 timestamp

As shown below: Yesterday: UNIX_TIMESTAMP(CAST(SY...

How to operate Docker and images

Find mirror We can search for images from the Doc...

Detailed Example of JavaScript Array Methods

Table of contents Introduction Creating an Array ...

iview implements dynamic form and custom verification time period overlap

Dynamically adding form items iview's dynamic...

An article to understand what is MySQL Index Pushdown (ICP)

Table of contents 1. Introduction 2. Principle II...

vue+tp5 realizes simple login function

This article example shares the specific code of ...

How to ensure the overall user experience

Related Articles: Website Design for User Experien...

React hooks introductory tutorial

State Hooks Examples: import { useState } from &#...

Is it true that the simpler the web design style, the better?

Original address: http://www.webdesignfromscratch...

Detailed example of sorting function field() in MySQL

Preface In our daily development process, sorting...

Several CSS3 tag shorthands (recommended)

border-radius: CSS3 rounded corners Syntax: borde...