JS implements random generation of verification code

JS implements random generation of verification code

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

<!DOCTYPE html>
<html>
 <head>
  <meta charset="utf-8" />
  <title></title>
  <style>   
  </style>
 </head>
 <body>
  <div>
  <h1>111111</h1><a href="#" rel="external nofollow" >Can't see clearly, change one</a>
  </div>
  Verification code: <input type="text" value="" />
  <button>OK</button>
 </body>
 <script>
  var arr = ["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",
       "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",
       0,1,2,3,4,5,6,7,8,9];
        var h1=document.querySelector("h1");
        var btn = document.querySelector("button");
        var change = document.querySelector("a");     
        var input = document.querySelector("input");         
         function fn(){
        var arr1=Math.floor(Math.random()*62);
        var arr2 = Math.floor(Math.random()*62);
        var arr3 = Math.floor(Math.random()*62);
        var arr4=Math.floor(Math.random()*62);
        var arr5=Math.floor(Math.random()*62);
        var arr6=Math.floor(Math.random()*62);
        var yz=""+arr[arr1]+arr[arr2]+arr[arr3]+arr[arr4]+arr[arr5]+arr[arr6];
            h1.innerHTML=yz;    
            btn.onclick=function(){
              alert(input.value);
             if(input.value==yz){
            alert("correct");
            }else{
             alert("error");
            }
           
            }
         }
        fn();
    change.addEventListener("click",fn);
 </script>
</html>

1. Math.floor() method

Math.floor(x)

The floor() method returns the largest integer less than or equal to x.

If the argument passed is an integer, the value is unchanged.

2. Math.random() method

The random() method returns a random number between 0 (inclusive) and 1 (exclusive).

The above two methods can be used to realize the function of taking random numbers

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:
  • JavaScript implements random generation of verification code and verification
  • 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)

<<:  Detailed explanation of MySql view trigger stored procedure

>>:  Detailed explanation of the example of Connect on the Socket (TCP) Client side from the Linux source code

Recommend

MySQL slow log online problems and optimization solutions

MySQL slow log is a type of information that MySQ...

Conflict resolution when marquee and flash coexist in a page

The main symptom of the conflict is that the FLASH...

Should I use distinct or group by to remove duplicates in MySQL?

Preface About the performance comparison between ...

HTML drawing user registration page

This article shares the specific implementation c...

Detailed explanation of CSS3 to achieve responsive accordion effect

I recently watched a video of a foreign guy using...

Detailed explanation of Linux zabbix agent deployment and configuration methods

1. Install zabbix-agent on web01 Deploy zabbix wa...

MySQL master-slave synchronization principle and application

Table of contents 1. Master-slave synchronization...

Solutions to the failure and invalidity of opening nginx.pid

Table of contents 1. Problem Description 2. Probl...

Tutorial on installing MySQL 5.6 using RPM in CentOS

All previous projects were deployed in the Window...

Detailed tutorial on how to monitor Nginx/Tomcat/MySQL using Zabbix

Table of contents Zabbix monitors Nginx Zabbix mo...

Javascript uses the integrity attribute for security verification

Table of contents 1. Import files using script ta...

MySQL 8.0.18 deployment and installation tutorial under Windows 7

1. Preliminary preparation (windows7+mysql-8.0.18...

Docker solution for logging in without root privileges

When you use the docker command for the first tim...

8 tips for Vue that you will learn after reading it

1. Always use :key in v-for Using the key attribu...