js implementation of verification code case

js implementation of verification code case

This article example shares the specific code of js to implement the verification code for your reference. The specific content is as follows

CSS code:

input{
            width: 200px;
            height: 32px;
            border: 1px solid #000;
            box-sizing: border-box;
        }
        #c1{
            vertical-align: middle;
            box-sizing: border-box;
            cursor: pointer;
        }
        #btn{
            display: block;
            margin-top: 20px;
            height: 32px;
            font-size: 16px;
 
        }

HTML code:

<div class="code">
        <input type="text" value="" id="inputValue" placeholder="Please enter the verification code (not case sensitive)">
        <canvas id="c1" width="100" height="30" style="border:1px solid black"></canvas>
        <br>
        <button id="btn">Submit</button>
</div>

js code:

Some jQuery methods are used, please remember to import jQuery first

$(function(){
            //Store random verification code var showNum = []
 
            draw(showNum)
 
            $("#c1").click(function(){
                draw(showNum)
            })
            $("#btn").click(function(){
                var s = $("#inputValue").val().toLowerCase()
                var s1 = showNum.join("")
                if (s==s1) {
                    alert("Submission successful")
                }else{
                    alert("Verification code error")
                }
                draw(showNum)
            })
 
 
            // Encapsulate a random verification code on the canvas function draw(showNum){
                // Get canvas
                var canvas = $("#c1")
                var ctx = canvas[0].getContext("2d")
                // Get the width and height of the canvas var canvas_width = canvas.width()
                var canvas_height = canvas.height()
                // Clear the previously drawn content // 0,0 The starting coordinates to be cleared // The width and height of the rectangle ctx.clearRect(0,0,canvas_width,canvas_height)
                // Start drawing var scode = "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,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,1,2,3,4,5,6,7,8,9,"
                var arrCode = scode.split(",")
                var arrLength = arrCode.length
                for(var i = 0;i<4;i++){
                    var index = Math.floor(Math.random()*arrCode.length)
                    var txt = arrCode[index] //Randomly select a character showNum[i] = txt.toLowerCase() //Convert to lowercase and store in verification code array //Start to control the drawing position of the character var x = 10 +20*i //The x coordinate of the starting point of each verification code drawing var y = 20 + Math.random()*8 //The y coordinate of the starting point ctx.font = "bold 20px Microsoft YaHei"
                    // Start rotating the character var deg = Math.random*-0.5
                    // To achieve the effect of tilting the drawn content, the canvas must be translated first, the purpose is to move the rotation point to the place where the content is drawn ctx.translate(x,y)
                    ctx.rotate(deg)
                    // Set the random color of the drawing ctx.fillStyle = randomColor()
                    ctx.fillText(txt,0,0)
 
                    // Restore the canvas ctx.rotate(-deg)
                    ctx.translate(-x,-y)
 
                }
                for(var i = 0;i<30;i++){
                    if (i<5) {
                        // Draw line ctx.strokeStyle = randomColor()
                        ctx.beginPath()
                        ctx.moveTo(Math.random()*canvas_width,Math.random()*canvas_height)
                        ctx.lineTo(Math.random()*canvas_width,Math.random()*canvas_height)
                        ctx.stroke()
                    }
                    // Draw points ctx.strokeStyle = randomColor()
                    ctx.beginPath()
                    var x = Math.random()*canvas_width
                    var y = Math.random()*canvas_height
                    ctx.moveTo(x,y)
                    ctx.lineTo(x+1,y+1)
                    ctx.stroke()
 
                }
 
 
            }
 
            // Random color function randomColor(){
                var r = Math.floor(Math.random()*256)
                var g = Math.floor(Math.random()*256)
                var b = Math.floor(Math.random()*256)
                return `rgb(${r},${g},${b})`
 
            }
 
        })

This is a random example. If there are any mistakes, please feel free to give me your advice.

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:
  • Jsp method to generate page verification code [with code]
  • Example code of JavaScript verification code (with effect diagram)
  • js implements simple verification code
  • js realizes the countdown effect of clicking to get the verification code
  • Three ways to implement JS verification code function
  • Implementation and technical analysis of verification code generated by js
  • js generates the verification code and judges it directly on the front end
  • js+canvas realizes the function of sliding puzzle verification code
  • JS makes graphic verification code implementation code
  • js implements the login registration box mobile phone number and verification code verification (front-end part)

<<:  Docker images export and import operations

>>:  Optimizing query speed of MySQL with tens of millions of data using indexes

Recommend

Comprehensive understanding of Node event loop

Table of contents Node Event Loop Event loop diag...

Detailed explanation of using MySQL where

Table of contents 1. Introduction 2. Main text 2....

Docker uses nextcloud to build a private Baidu cloud disk

Suddenly, I needed to build a private service for...

Introduction to major browsers and their kernels

Trident core: IE, MaxThon, TT, The World, 360, So...

Create a code example of zabbix monitoring system based on Dockerfile

Use the for loop to import the zabbix image into ...

Analysis of MySQL cumulative aggregation principle and usage examples

This article uses examples to illustrate the prin...

Practical Optimization of MySQL Paging Limit

Preface When we use query statements, we often ne...

Solution to the low writing efficiency of AIX mounted NFS

Services provided by NFS Mount: Enable the /usr/s...

MYSQL subquery and nested query optimization example analysis

Check the top 100 highest scores in game history ...

Examples of using Docker and Docker-Compose

Docker is an open source container engine that he...

Complete step record of vue encapsulation TabBar component

Table of contents Implementation ideas: Step 1: C...

HTML hyperlink a tag_Powernode Java Academy

Anyone who has studied or used HTML should be fam...

Six weird and useful things about JavaScript

Table of contents 1. Deconstruction Tips 2. Digit...