canvas实现随机验证码
canvas实现随机验证码
知识点
- canvas生成背景图和文字 设置字体样式和大小
- String的fromCharCode(code码)生成大小写字母和数字 str.toLowerCase()转小写
- 随机抽取不重复的6位数字组成验证码字符串
- 效果图
html:
<div class="wraper">
<input type="text" maxlength="6" placeholder="请输入验证码,不区分大小写" class="input">
<span class="icon"></span>
<p class="error">验证码输入有误,请重新输入</p>
<div class="canvas-box">
<canvas id="myCanvas" width="200" height="50"></canvas>
<input type="button" class="refresh">
</div>
<div class="btn">
<button class="submit">提交</button>
</div>
</div>
css:
* {
margin: 0;
padding: 0;
}
html,
body {
width: 100%;
height: 100%;
background: hotpink;
display: flex;
justify-content: center;
align-items: center;
}
.wraper {
width: 345px;
margin: 30px;
padding: 15px;
border-radius: 5px;
border: 1px solid #ccc;
background: #fff;
}
.input {
width: 300px;
padding: 15px;
border-radius: 5px;
border: 1px solid #ccc;
box-sizing: border-box;
}
.icon {
float: right;
width: 20px;
height: 20px;
margin-top: 13px;
background: url('./img/yes.png') no-repeat;
background-size: 100% 100%;
display: none;
}
.error {
margin-top: 10px;
color: red;
font-size: 12px;
display: none;
}
.canvas-box {
margin-top: 15px;
position: relative;
}
#myCanvas {
width: 300px;
height: 60px;
}
.canvas-box .refresh {
position: absolute;
right: 0;
top: 50%;
margin-top: -10px;
display: inline-block;
width: 20px;
height: 20px;
background: url('./img/refresh.png') no-repeat;
background-size: 100% 100%;
border: none;
outline: none;
cursor: pointer;
}
.btn {
margin-top: 15px;
}
.btn .submit {
width: 80px;
height: 40px;
border-radius: 5px;
background: blue;
color: #fff;
border: none;
outline: none;
cursor: pointer;
}
js:
var arr = []; //筛选验证码的数组
var value = '';
//48-57 数字 65-90 大写字母 97-122 小写字母
for (var i = 48; i <= 57; i++) {
arr.push(String.fromCharCode(i));
}
for (let i = 65; i <= 90; i++) {
arr.push(String.fromCharCode(i));
}
for (let i = 97; i <= 122; i++) {
arr.push(String.fromCharCode(i));
}
//生成随机验证码
function getVerification() {
var codeStr = '';
var codeArr = [];
value = '';
while (true) {
let a = Math.floor(Math.random() * arr.length);
if (codeArr.indexOf(a) == -1) {
codeArr.push(arr[a]);
}
if (codeArr.length == 6) {
break;
}
}
codeStr = codeArr.join(' ');
value = codeArr.join('').toLowerCase();
console.log(value)
var myCanvas = document.getElementById('myCanvas');
var ctx = myCanvas.getContext('2d');
var img = new Image();
img.src = './img/bg_pic.jpg';
img.onload = function() {
var pat = ctx.createPattern(img, 'no-repeat');
ctx.fillStyle = pat;
ctx.fillRect(0, 0, myCanvas.width, myCanvas.height);
ctx.textAlign = 'center';
ctx.fillStyle = '#ccc';
ctx.font = '30px Roboto Slab';
ctx.setTransform(1, -0.12, 0.3, 1, 0, 12);
ctx.fillText(codeStr, myCanvas.width / 2, 35);
}
}
getVerification();
//事件
var refresh = document.getElementsByClassName('refresh')[0];
var submit = document.getElementsByClassName('submit')[0];
var inputValue = document.getElementsByClassName('input')[0];
var icon = document.getElementsByClassName('icon')[0];
var error = document.getElementsByClassName('error')[0];
refresh.onclick = function() {
getVerification();
}
submit.onclick = function() {
if (inputValue.value.toLowerCase() === value) {
icon.style.display = 'inline-block';
icon.style.background = "url('./img/yes.png') no-repeat";
icon.style.backgroundSize = "100% 100%";
error.style.display = 'none';
getVerification();
} else {
icon.style.display = 'inline-block';
icon.style.background = "url('./img/error.png') no-repeat";
icon.style.backgroundSize = "100% 100%";
error.style.display = 'block';
inputValue.value = '';
}
}
参考至腾讯课堂渡一教育
canvas实现随机验证码的更多相关文章
- canvas制作随机验证码
看到人家彩色背景的验证码想测试一下: 创建html代码: <canvas id="myCanvas" width="200" height="1 ...
- 用Canvas生成随机验证码(后端前端都可以)
一 .使用前端生成验证码 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> ...
- canvas绘制随机验证码
效果图: 思路: 1, 绘制canvas画布,进行基础设置 2.绘制一个矩形 3.设置验证码的随机数 4.设置验证码随机数的随机颜色 5.绘制随机干扰线 6,绘制随机干扰点 经过以上六个步骤,验证码的 ...
- 微信小程序 canvas 生成随机验证码
转载:https://blog.csdn.net/qq_16646819/article/details/81020245?utm_source=blogxgwz0 js // pages/bind/ ...
- Android实现随机验证码——自定义View
一.问题描述 熟悉web开发中童鞋们都知道为了防止恶意破解.恶意提交.刷票等我们在提交表单数据时,都会使用随机验证码功能.在Android应用中我们同样需要这一功能,该如何实现呢,下面我们就自定义一个 ...
- JavaScript随机验证码
利用canvas制作一个随机验证码: 1.clearRect:context.clearRect(x,y,width,height);清空给定矩形内的指定像素 2.fillStyle:设置画笔的颜色 ...
- PYTHON 随机验证码生成
# 生成一个六位随机验证码 import random # random 生成随机数 temp = '' for i in range(6): num = random.randrange(0,6) ...
- Java生成随机验证码
package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- js用正则表达式验证用户和密码的安全性,生成随机验证码
制作了一个表单,表单验证用户.密码.随机验证码 html页面
随机推荐
- 如何在自定义组件中使用v-model
文章属于速记,有错误欢迎指出.风格什么的不喜勿喷. 先来一个组件,不用vue-model,正常父子通信 <!-- parent --> <template> <div c ...
- leetcode_1. Two Sum
leetcode_1. Two Sum 前言: 这段时间开始敲leetcode.我认为这并不仅仅只是为了应付笔试,面试.而是确实有着一定的意义. 尤其,你提交代码后,网站会多方面验证你的答案. 另外, ...
- 一分钟告诉你究竟DevOps是什么鬼?
历史回顾 为了能够更好的理解什么是DevOps,我们很有必要对当时还只有程序员(此前还没有派生出开发者,前台工程师,后台工程师之类)这个称号存在的历史进行一下回顾. 如编程之道中所言: 老一辈的程序员 ...
- Centos7搭建hadoop完全分布式
虽然说是完全分布式,但三个节点也都是在一台机器上.拿来练手也只能这样咯,将就下.效果是一样滴.这个我自己都忘了步骤,一起来回顾下吧. 必备知识: Linux基本命令 vim基本命令 准备软件: VMw ...
- webstorm-快捷键大全
Webstorm快捷键 Eclipse快捷键 说明 ctrl+shift+N ctrl+shift+R 通过文件名快速查找工程内的文件(必记) ctrl+shift+alt+N ctrl+shift+ ...
- 使用Spring Session实现Spring Boot水平扩展
小编说:本文使用Spring Session实现了Spring Boot水平扩展,每个Spring Boot应用与其他水平扩展的Spring Boot一样,都能处理用户请求.如果宕机,Nginx会将请 ...
- 如何在Android Studio中指定NDK位置?
如何在Android Studio中指定NDK位置? 问题描述 NDK已经手工下载解包在本地: D:\Portable\android-ndk-r13b 每次创建支持C++项目时,都提示NDK没配置, ...
- php开启fileinfo扩展
1.检查当前环境: php -i|grep fileinfo 1 看是否已安装fileinfo扩展,若没有,则进行下一步. 2.安装fileinfo扩展 2.1.下载扩展包 根据各自的版本号进行下载 ...
- 我TM菜爆
我怎么什么都能爆零啊! 我太神了!
- 【bzoj 4173】数学
Description Input 输入文件的第一行输入两个正整数 . Output 如题 Sample Input 5 6 Sample Output 240 HINT N,M<=10^15 ...