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
First, let's talk about the in() query. It is...
Preface I believe most people have used MySQL and...
XMeter API provides a one-stop online interface t...
Table of contents 1. Teleport usage 2. Complete t...
Table of contents Select Structure Loop Structure...
undefined In JavaScript, if we want to determine ...
1. Previous versions yum remove docker docker-cli...
vue-element-admin import component encapsulation ...
Many friends have always wanted to know how to ru...
Basics In a relational database, each data table ...
Table of contents 1. Select All 2. Increase or de...
lead Some common triangles on web pages can be dr...
Related articles: Install Docker using yum under ...
Recently, when using Apple.com/Ebay.com/Amazon.co...
The final solution is in the last picture If you ...