Web开发中的验证码生成插件
Vcode.js
开源地址:https://github.com/JofunLiang/Vcode.js
Vcode.js是一个基于原生Javascript开发的轻量级验证码生成插件
先来看几张效果图
- 只包含数字
纯数字的验证码
- 只包含字母
纯字母的验证码
- 既包含数字又包含字母
同时包含数字和字母的验证码
- 自定义的数据
自定义数据的验证码
使用方法(官网下载js文件):
<script src="Vcode.min.js"></script> //OR // ES6 Modules or TypeScript import vcode from 'Vcode' // CommonJS const vcode = require('Vcode')
Demo:
<div id="demo" class="demo"> <strong>验证码:</strong> <div class="code"></div> <button type="button">换一个</button> </div> function $(selector) { return document.querySelector(selector); } //纯数字模式 var vcode = new Vcode({ el: "#demo .code", count: 4, fontSize: "60px", type: "number", spacing: 0 }); //纯字母模式 var vcode = new Vcode({ el: "#demo .code", count: 4, fontSize: "60px", type: "letter", spacing: 0 }); //字母数字混合模式 var vcode = new Vcode({ el: "#demo .code", count: 4, fontSize: "60px", spacing: 0 }); //自定义数据模式 var vcode = new Vcode({ el: "#demo .code", data: "马蹄声回荡在竹林间千山外江湖边月光纵横云掠过山鹰的背脊他冷眼看繁花乱掷地无声", count: 4, fontSize: "42px", spacing: 0 }); //重绘验证码 $("#demo button").onclick = function() { vcode.onReset(); };
参数设置:
- el(字符串,必填项) css选择器,用来填充验证码;
- data(字符串,可选项)自定义验证码的数组来源;
- count(整形数字,可选项,默认是4)生成验证码的字符长度;
- type(字符串,可选项,默认是mix,数字字母混合模式)验证码类型;
- fontSize(字符串,可选项)验证码的字体大小;
- spacing(字符串,可选项,默认0)验证码字符间的间距;
验证码对象属性:
- el(对象)验证码挂载的对象
- code(字符串)验证码值
- data(字符串)验证码数据源
- count(数字)验证码长度
- onReset(函数)重新生成验证码函数
MIT License
Copyright (c) 2018 jofunLiang
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
同类插件
- Vue验证码插件vue2-verify(https://github.com/mizuka-wu/vue2-verify)
- 基于js和canvas的captcha.js (https://github.com/saucxs/captcha)