溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

js+canvas如何實現(xiàn)驗證碼功能

發(fā)布時間:2021-05-12 13:12:05 來源:億速云 閱讀:157 作者:小新 欄目:web開發(fā)

小編給大家分享一下js+canvas如何實現(xiàn)驗證碼功能,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

剛剛開始接觸canvas,寫個驗證碼小功能練練手,實現(xiàn)效果圖如下:

js+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);
})

以上是“js+canvas如何實現(xiàn)驗證碼功能”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI