JavaScript function encapsulates random color verification code (complete code)

JavaScript function encapsulates random color verification code (complete code)

An n-digit verification code consisting of numbers, letters, or a mixture of numbers and letters with random colors. Below is the complete code, take it if you need it!

​
function verify(a = 6,b = "num"){
  //Define three random verification codes verification code library var num = "0123456789"
  var str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNIPQRSTUVWXYZ"
  var mixin = num + str;
  
  //Define an empty string to store the verification code var verify=""
  if(a == undefined || b == undefined){
    //Verify whether the input is legal. If it fails, throw an exception. throw new Error("Parameter exception");
  }else{
      if(a == "" || b==""){
        //Judge whether the user has not input throw new Error("Illegal parameter.");
      }else{
        //Detect the input type to determine whether to enter var typea = typeof(a);
        var typeb = typeof(b);
        if(typea == "number" && typeb == "string"){
            if(b == "num"){
                  
                //Define a loop to receive the verification code pure digital verification code for(var i=0;i<a;i++){
                     //Define a variable to store the random value of color var r1 = Math.random()*255;
                     var g1 = Math.random()*255;
                     var b1 = Math.random()*255;
 
                     //Determine the random index var index = Math.floor(Math.random()*(num.length-1))
                     //Determine the random verification code var char = num[index];
                     //Add color to the random verification code verify += `<span style ='color:rgb(${r1},${g1},${b1})'>${char}</span>`
                 }
                 //Return to the array itself return verify;
            }else if(b == "str"){
                 for(var i=0;i<a;i++){
                   //Verification code of pure letters var r1 = Math.random()*255;
                    var g1 = Math.random()*255;
                    var b1 = Math.random()*255;
 
                    var index = Math.floor(Math.random()*(str.length-1));
                    var char = str[index];
 
                    verify += `<span style ='color:rgb(${r1},${g1},${b1})'>${char}</span>`
                 }
                 return verify;   
            }else if(b == "mixin"){
                 // Mixed verification code for(var i=0;i<a;i++){
                    var r1 = Math.random()*255;
                    var g1 = Math.random()*255;
                    var b1 = Math.random()*255;
 
                    var index = Math.floor(Math.random()*(mixin.length-1));
                    var char = mixin[index];
 
                    verify += `<span style ='color:rgb(${r1},${g1},${b1})'>${char}</span>`
                }
                return verify;
            }else{
                //If the verification fails, an exception is thrown. throw new Error("Input type is illegal.")
            }
        
        }else{
            //If the verification fails, an exception is thrown. throw new Error("Input type is illegal.")
        }
      }
  }
}
 
​

Let's try calling the function

  //The first value is the length entered by the user, and the second is the type! 
 var arr = verify(8,"mixin");
     document.write(arr) 

Above is the result!

I have recorded this for the convenience of future use. I also hope that the big guys will communicate more, leave more messages, and point out my shortcomings!

Friends in need can study it! !

This is the end of this article about JavaScript function encapsulation of random color verification code. For more related js function encapsulation color verification code content, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • JS implements random generation of verification code
  • 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

<<:  33 ice and snow fonts recommended for download (personal and commercial)

>>:  CSS sample code with search navigation bar

Recommend

js to realize the rotation of web page pictures

This article shares the specific code of js to re...

How to use MySQL binlog to restore accidentally deleted databases

Table of contents 1 View the current database con...

Web page HTML ordered list ol and unordered list ul

Lists for organizing data After learning so many ...

Detailed explanation of the order of JS object traversal

Some of you may have heard that the order of trav...

How to use SessionStorage and LocalStorage in Javascript

Table of contents Preface Introduction to Session...

JavaScript to filter arrays

This article example shares the specific code for...

Markup language - specify CSS styles for text

Click here to return to the 123WORDPRESS.COM HTML ...

A quick solution to the first login failure in mysql5.7.20

First, we will introduce how (1) MySQL 5.7 has a ...

Solution to the Docker container cannot be stopped and deleted

Find the running container id docker ps Find the ...

js implements clock component based on canvas

Canvas has always been an indispensable tag eleme...