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:
|
<<: 33 ice and snow fonts recommended for download (personal and commercial)
>>: CSS sample code with search navigation bar
This article shares the specific code of js to re...
Table of contents 1 View the current database con...
Detailed explanation of the solution to garbled c...
Preface The concept of dark mode originated from ...
Lists for organizing data After learning so many ...
Some of you may have heard that the order of trav...
Table of contents Preface Introduction to Session...
This article example shares the specific code for...
Preface At work, we often need to operate in a Li...
Click here to return to the 123WORDPRESS.COM HTML ...
First, we will introduce how (1) MySQL 5.7 has a ...
Find the running container id docker ps Find the ...
Canvas has always been an indispensable tag eleme...
For example, if I have a Jenkins server in my int...
Table of contents 1. Background of the problem: 2...