欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

js+canvas实现验证码功能

程序员文章站 2022-05-16 13:16:09
刚刚开始接触canvas,写个验证码小功能练练手,实现效果图如下: 主要代码如下: html

刚刚开始接触canvas,写个验证码小功能练练手,实现效果图如下:

js+canvas实现验证码功能

主要代码如下:

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));
}
//每个验证码可能出现的字母或数字(区分大小写)
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;
  }
}

//点击提交按钮时的结果
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);
})

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。