This article shares the specific code for JavaScript to achieve the verification code effect for your reference. The specific content is as follows Today's case, the effect is as follows: There are actually not many difficulties in implementing this case. Let's take a look at it together. The implementation of html and css will not be explained here. You can implement it yourself by comparing the following code and pay attention to the details. Next, let's take a look at the implementation of js: There are two things we need to do: 1. Realize the random generation of verification codes so that they can be generated when the page is refreshed or clicked to change First , we need to use a for loop and Second , we only need to get the string entered by the user through Other details can be found in the code. <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <style> .container{ width: 400px; height: 100px; margin:100px auto; background-color: hsla(180, 73%, 78%, 0.199); border-radius: 20px; text-align: center; padding: 20px; } #check{ display: inline-block; width: 100px; height: 30px; text-align: center; background-color: rgba(128, 128, 128, 0.158); color:blue; font-size:26px; font-style: italic; letter-spacing: 2px; font-family:Arial, Helvetica, sans-serif; margin-bottom: 10px; } .ma{ margin-bottom: 12px; } </style> </head> <body> <div class="container"> <div> <span id="check">adf34y</span> <a href="#" id="checka">If it's not clear, change one</a> </div> <div class="ma"> <label for="zheng">Verification code</label> <input type="text" id="zheng"> </div> <button id="btn">OK</button> </div> <script> let sum=[0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f']; var check = document.getElementById("check"); var code; function fun(){ let str=""; for(let i=0;i<6;i++){ let pos=Math.round(Math.random()*15); //Note this way of writing, get the random number str+=sum[pos]; } check.innerHTML=str; return str; } window.onload = function(){ code=fun(); } let checka = document.getElementById("checka"); checka.onclick=function(){ code=fun(); } let btn = document.getElementById("btn"); btn.onclick=function(){ let t=document.getElementById("zheng").value; console.log(t) if(t==code){ alert("correct"); code=fun(); document.getElementById("zheng").value=""; } else{ alert("error"); document.getElementById("zheng").value=""; } } </script> </body> </html> 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:
|
<<: Understand the use of CSS3's all attribute
>>: Cross-host communication between docker containers-overlay-based implementation method
Some people say that IE9 is Microsoft's secon...
Table of contents 01 What is Kubernetes? 02 The d...
I recently configured a server using Tencent Clou...
The search binary tree implementation in JavaScri...
Meta tag function The META tag is a key tag in th...
1. Overflow Overflow is overflow (container). Whe...
In the pages of WEB applications, tables are ofte...
1. Create users and authorize Creating users and ...
HTML 4 HTML (not XHTML), MIME type is text/html, ...
content Use scaffolding to quickly build a node p...
The explain command is the primary way to see how...
Table of contents Overview Getting started with d...
MySQL 5.7.27 detailed download, installation and ...
This article shares the specific code of JS+Canva...
Description and Introduction Docker inspect is a ...