Layui implements the login interface verification code

Layui implements the login interface verification code

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

Effect picture:

html:

<div class="layui-form-item">
    <div class="layui-col-xs6" >
      <input type="text" value="" placeholder="Please enter the verification code (not case sensitive)" class="input-val"> 
      <canvas id="canvas" width="100" height="30"></canvas>  
     </div>
      <div>
      <input type="button" value="Login" class="layui-btn layui-btn-fluid" lay-submit lay-filter="login"> 
      </div>         
</div>

Next is JS:

var show_num=[];
  $(function()
  {
   draw(show_num);
   $("#canvas").on('click',function()
   {
  draw(show_num);      
   })      
  });

Then call two functions:

function draw(show_num) {
            var canvas_width = $('#canvas').width();
            var canvas_height = $('#canvas').height();
            var canvas = document.getElementById("canvas"); //Get the canvas object, the actor var context = canvas.getContext("2d"); //Get the canvas drawing environment, the actor's performance stage canvas.width = canvas_width;
            canvas.height = canvas_height;
            var sCode = "A,B,C,E,F,G,H,J,K,L,M,N,P,Q,R,S,T,W,X,Y,Z,1,2,3,4,5,6,7,8,9,0";
            var aCode = sCode.split(",");
            var aLength = aCode.length; //Get the length of the array for (var i = 0; i <= 3; i++) {
                var j = Math.floor(Math.random() * aLength); //Get a random index value var deg = Math.random() * 30 * Math.PI / 180; //Generate a random radian between 0 and 30 var txt = aCode[j]; //Get a random content show_num[i] = txt.toLowerCase();
                var x = 10 + i * 20; //x coordinate of the text on the canvasvar y = 20 + Math.random() * 8; //y coordinate of the text on the canvascontext.font = "bold 23px Microsoft YaHei";

                context.translate(x, y);
                context.rotate(deg);

                context.fillStyle = randomColor();
                context.fillText(txt, 0, 0);

                context.rotate(-deg);
                context.translate(-x, -y);
            }
            for (var i = 0; i <= 5; i++) { //Display lines on the verification code context.strokeStyle = randomColor();
                context.beginPath();
                context.moveTo(Math.random() * canvas_width, Math.random() * canvas_height);
                context.lineTo(Math.random() * canvas_width, Math.random() * canvas_height);
                context.stroke();
            }
            for (var i = 0; i <= 30; i++) { //Show small dots on the verification code context.strokeStyle = randomColor();
                context.beginPath();
                var x = Math.random() * canvas_width;
                var y = Math.random() * canvas_height;
                context.moveTo(x, y);
                context.lineTo(x + 1, y + 1);
                context.stroke();
            }
        }

        function randomColor() { //Get a random color value 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 + ")";
        }

The style must be adjusted according to the project. Here is my style:

<style>
        .code {
            width: 100%;
            margin: 0 auto;
        }
        .input-val {
            width: 63%;
            background: #ffffff;
            height: 2.8rem;
            padding: 0 2%;
            border-radius: 5px;
            border: none;
            border: 1px solid rgba(0,0,0,.2);
            font-size: 0.9rem;
        }
        #canvas {
            float: right;
            display: inline-block;
            border: 1px solid #ccc;
            border-radius: 5px;
            cursor: pointer;
        }
</style>

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:
  • layUI's verification code function and verification example

<<:  HTML dynamically loads css styles and js scripts example

>>:  Linux operation and maintenance basic process management real-time monitoring and control

Recommend

Html+CSS drawing triangle icon

Let’s take a look at the renderings first: XML/HT...

Summary of several commonly used CentOS7 images based on Docker

Table of contents 1 Install Docker 2 Configuring ...

Skin change solution based on Vue combined with ElementUI

Table of contents Written in front Solution 1: Us...

Docker Swarm from deployment to basic operations

About Docker Swarm Docker Swarm consists of two p...

20 Signposts on the Road to Becoming an Excellent UI (User Interface) Designer

Introduction: Interface designer Joshua Porter pub...

JavaScript article will show you how to play with web forms

1. Introduction Earlier we introduced the rapid d...

How to monitor mysql using zabbix

Zabbix deployment documentation After zabbix is ​...

How to use partitioning to optimize MySQL data processing for billions of data

When MySQL queries tens of millions of data, most...

JavaScript to achieve simple drag effect

This article shares the specific code of JavaScri...

How to use JS code compiler Monaco

Preface My needs are syntax highlighting, functio...