SoFunction
Updated on 2025-04-10

JS implements random verification code function

1. Verification code Verification is a common verification point on web pages. There are many types of verification codes. The following code only implements a simple verification function.

 <div> 
  <input type = "text" id = "input" value="" /> 
  <input type = "button"  onclick="createCode()"/> 
  <input type = "button" value = "verify" onclick = "validate()"/> 
 </div> 

2. Add some styles

 #code{ 
    font-family:Arial; 
    font-style:italic; 
    font-weight:bold; 
    border:0; 
    letter-spacing:2px; 
    color:blue; 
   }

I've added detailed notes in the part

//Set a global variable to facilitate the storage of verification code var code;
 function createCode(){
  //First, the default code is an empty string  code = '';
  //Set the length, depending on the requirements here, I have set 4 here  var codeLength = 4;
  var codeV = ('code');
  //Set random characters  var random = new Array(0,1,2,3,4,5,6,7,8,9,'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');
  //Loop codeLength The 4 I set is to loop 4 times  for(var i = 0; i < codeLength; i++){
   //Set the random number range, this is set to 0 ~ 36    var index = (()*36);
    //Stand String Stitching Splice the random characters each time    code += random[index]; 
  }
  // Assign the spliced ​​string to the displayed Value   = code;
 }
 //The following is the code to determine whether ==, no explanation is required function validate(){
  var oValue = ('input').();
  if(oValue ==0){
   alert('Please enter the verification code');
  }else if(oValue != code){
   alert('The verification code is incorrect, please re-enter');
   oValue = ' ';
   createCode();
  }else{
   ('','_self');
  }
 }
 //The reason for setting this is that each time you enter the interface, it will be empty if you don't set it  = function (){
  createCode();
 }

js verification code topic reference:https:///Special/

The above is the JS implementation of random verification code function introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!