js+canvas實現(xiàn)驗證碼功能
更新時間:2020年09月21日 15:43:33 作者:灑-脫
這篇文章主要為大家詳細(xì)介紹了js+canvas實現(xiàn)驗證碼功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
剛剛開始接觸canvas,寫個驗證碼小功能練練手,實現(xiàn)效果圖如下:

主要代碼如下:
html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <link rel="stylesheet" href="index.css" rel="external nofollow" > </head> <body> <div class="wrapper"> <div class="inputBox"> <input type="text" class = 'inputCaptcha' placeholder = "請輸入驗證碼"> <span class="captchaIcon"></span> </div> <p class="errorText"></p> <div class="canvasBox"> <div class="imageBox"> <canvas width =300 height=80 id = 'canvasCaptcha'></canvas> <input type="button" class='refresh'> </div> </div> <button class="captchaSubmit">submit</button> </div> <script src='./jquery.js'></script> <script src = './index.js'></script> </body> </html>
css
* {
margin: 0;
padding: 0;
}
.wrapper {
width: 345px;
border: 1px solid #ccc;
border-radius: 10px;
padding: 20px 10px;
margin: 30px 30px;
}
.inputBox {
width: 345px;
margin: 0 auto 10px;
position: relative;
}
.inputBox .inputCaptcha {
display: inline-block;
height: 50px;
width: 86%;
text-indent: 1em;
border: 1px solid #ddd;
border-radius: 5px;
}
.inputBox .captchaIcon {
display: none;
position: absolute;
top: 50%;
right: 0px;
margin-top: -16px;
width: 32px;
height: 32px;
background-size: 100% 100%;
}
.errorText {
width: 345px;
margin: 0 auto;
font-size: 12px;
color: red;
display: none;
}
.canvasBox {
width: 345px;
margin: 10px auto;
position: relative;
}
#canvasCaptcha {
border-radius: 10px;
}
.canvasBox .refresh {
width: 34px;
height: 34px;
position: absolute;
right: 0px;
top: 50%;
margin-top: -17px;
border: 0;
border-radius: 6px;
background-image: url('./images/update.png');
background-size: cover;
}
.captchaSubmit {
padding: 10px 20px;
background-color: #62b900;
border: 0;
font-size: 20px;
border-radius: 5px;
color: #fff;
}
js
var arr = [0,1,2,3,4,5,6,7,8,9];
for(var i = 65;i < 122;i++){
if(i>90&&i<97){
continue;
}
arr.push(String.fromCharCode(i));
}
//每個驗證碼可能出現(xiàn)的字母或數(shù)字(區(qū)分大小寫)
var len = arr.length;
//驗證碼的長度
var canvasStr,value;
//驗證碼值
function createCanvas(){
canvasStr = '';
value = '';
for(var i =0 ;i < 6;i++){
var a = arr[Math.floor(Math.random()*len)];
canvasStr += a+' ';
value += a;
}
//canvas
var oCanvas = document.getElementById('canvasCaptcha');
var ctx = oCanvas.getContext('2d');
var w = oCanvas.width;
var h = oCanvas.height;
var oImg = new Image();
oImg.src = './images/bg.jpg';
oImg.onload = function(){
var pattern = ctx.createPattern(oImg,'repeat');
ctx.fillStyle = pattern;
ctx.fillRect(0,0,w,h);
ctx.fillStyle = '#ccc';
ctx.textAlign = 'center';
ctx.font = '46px Roboto Slab';
ctx.setTransform(1,-0.12,0.2,1,0,12);
ctx.fillText(canvasStr,w/2,60);
}
}
createCanvas();
//驗證輸入的驗證碼與圖中驗證碼時候相等
function captcha(e){
if(e == value){
return true;
}
else{
return false;
}
}
//點擊提交按鈕時的結(jié)果
function showResult(){
var inputValue = $('.inputCaptcha').val();
if(inputValue == '' ||inputValue == null || inputValue == 'undefined'){
$('.errorText').css({display:'inline-block'}).html('驗證碼不能為空,請重新輸入!');
$('.captchaIcon').css({display:'inline-block',backgroundImage:"url('./images/false.png')"});
}else{
var flag = captcha(inputValue);
if(!flag){
$('.errorText').css({display:'inline-block'}).html('驗證碼錯誤,請重新輸入!');
$('.captchaIcon').css({display:'inline-block',backgroundImage:"url('./images/false.png')"});
}else{
$('.captchaIcon').css({display:'inline-block',backgroundImage:"url('./images/true.png')"});
}
}
}
$('.captchaSubmit').click(showResult);//提交
$('.refresh').click(createCanvas);//刷新
//點擊input框
$('.inputCaptcha').focus(function(){
$('.errorText').add($('.captchaIcon')).fadeOut(100);
})
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Raphael帶文本標(biāo)簽可拖動的圖形實現(xiàn)代碼
Javascript和Raphael順便學(xué)習(xí)了一下,主要是為了實現(xiàn)一個可拖動的矩形同時矩形上還得顯示標(biāo)簽,網(wǎng)上關(guān)于這方面的知識提的很是于是本人自不量力寫了一下,感興趣的你可不要錯過了哈,希望可以幫助到你2013-02-02
JS實現(xiàn)提交表單前的數(shù)字及郵箱校檢功能
在項目開發(fā)中經(jīng)常會遇到表單提交功能,今天小編抽空給大家分享JS實現(xiàn)提交表單前的數(shù)字及郵箱校檢功能,需要的朋友參考下吧2017-11-11
關(guān)于Javascript中defer和async的區(qū)別總結(jié)
相信看過javascript高級程序設(shè)計的人,在javascript高級程序設(shè)計里,應(yīng)該看到了介紹了有關(guān)defer和async的區(qū)別,可是比較淺顯,而且也說得不是很清楚。下面我們來通過這篇文章來詳細(xì)了解下dfer和async的區(qū)別。2016-09-09
使用?JavaScript?Promise?讀取?Github?用戶數(shù)據(jù)
這篇文章主要介紹了使用JavaScript?Promise讀取Github用戶數(shù)據(jù),文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-08-08

