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

Web Design Teaching or Learning Program

Section Course content Hours 1 Web Design Overvie...

How to use Baidu Map API in vue project

Table of contents 1. Register an account on Baidu...

How to view Linux ssh service information and running status

There are many articles about ssh server configur...

How to monitor and delete timed out sessions in Tomcat

Preface I accidentally discovered that the half-h...

What to do if you forget the initial password of MySQL on MAC

The solution to forgetting the initial password o...

Basic use of subqueries in MySQL

Table of contents 1. Subquery definition 2. Subqu...

How to solve the problem of ERROR 2003 (HY000) when starting mysql

1. Problem Description When starting MYSQL, a pro...

Steps to deploy Docker project in IDEA

Now most projects have begun to be deployed on Do...

Mysql anonymous login cannot create a database problem solution

Frequently asked questions Access denied for user...

Monitor the size change of a DOM element through iframe

A common problem encountered during the developme...

How to install mysql5.6 in docker under ubuntu

1. Install mysql5.6 docker run mysql:5.6 Wait unt...

The hottest trends in web design UI in 2013 The most popular UI designs

Time flies, and in just six days, 2013 will becom...

Solve the problem of combining AND and OR in MySQL

As shown below: SELECT prod_name,prod_price FROM ...

Sample code for programmatically processing CSS styles

Benefits of a programmatic approach 1. Global con...