JavaScript implements random generation of verification code and verification

JavaScript implements random generation of verification code and verification

This article shares the specific code of JavaScript to achieve random generation of verification codes and verification for your reference. The specific content is as follows

Enter the verification code (case sensitive) and click Confirm to verify. Pop-up window prompts when an error occurs

Click to regenerate a random verification code

Prompt when the verification code is entered incorrectly

<body>
    <div class="v_code">
        <div class="code_show">
            <span class="code" id="checkCode"></span>
            <a href="#" id="linkbt">Can't see clearly, change one</a>
        </div>
        <div class="input_code">
            <label for="inputCode">Verification code:</label>
            <input type="text" id="inputCode">
            <span id="text_show"></span>
        </div>
        <input type="button" id="Button1" value="Confirm">
    </div>
    <script>
        // 1. Generate verification code // 6 digits 0-9 af Randomly generate 6 digits must be 0-9 af string // Array subscript 0, 1, 2... Randomly subscript 0-15 from the array // 2. Verify and click Confirm to compare window.onload = function() {
            const randomWord = () => {
                let code = '';
                for (var i = 0; i < 6; i++) {
                    var type = getRandom(1,3);
                    switch(type) {
                        case 1:
                            code += String.fromCharCode(getRandom(48,57)) // digital break;
                        case 2:
                            code += String.fromCharCode(getRandom(65,90)); //capital break;
                        case 3:
                            code += String.fromCharCode(getRandom(97,122)); //lowercase letter break;
                    }
                }
                return code;
            }
            function getRandom(min, max) {
                return Math.round(Math.random()*(max-min)+min)
            }

            //Call the data retrieval function const rand = randomWord();
            //console.log(rand);
            var checkCode = document.getElementById('checkCode');
            checkCode.innerText = rand;
        
        // Click to switch random number var linkbt = document.getElementById('linkbt');
            linkbt.addEventListener('click', function() {
                checkCode.innerText = randomWord();
            })

        // Submit for comparison document.getElementById('Button1').onclick = function() {
                var inputCode = document.querySelector('#inputCode');
                if (inputCode.value != checkCode.innerText) {
                    alert('The verification code you entered is incorrect');
                    inputCode.value = '';
                    return false;
                } else {
                    alert('Input is correct');
                }
            }
        }
    </script>
</body>

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 generation of verification code
  • JavaScript implements the generation of 4-digit random verification code
  • JavaScript clicks the button to generate a 4-digit random verification code
  • JavaScript function encapsulates random color verification code (complete code)

<<:  Docker Compose network settings explained

>>:  Mysql Workbench query mysql database method

Recommend

How to view Linux ssh service information and running status

There are many articles about ssh server configur...

A brief explanation of the reasonable application of table and div in page design

At the beginning of this article, I would like to ...

About the location of the H1 tag in XHTML

There has been a lot of discussion about H1 recent...

Analysis of MySQL lock mechanism and usage

This article uses examples to illustrate the MySQ...

MySQL starts slow SQL and analyzes the causes

Step 1. Enable MySQL slow query Method 1: Modify ...

Detailed steps to build an NFS file sharing server in Linux

Linux builds NFS server In order to achieve data ...

An example of using CSS methodologies to achieve modularity

1. What are CSS methodologies? CSS methodologies ...

Vue.js style layout Flutter business development common skills

Correspondence between flutter and css in shadow ...

Summary and practice of javascript prototype chain diagram

Table of contents Prototype chain We can implemen...

MySql Sql optimization tips sharing

One day I found that the execution speed of a SQL...

Teach you how to build hive3.1.2 on Tencent Cloud

Environment Preparation Before starting any opera...