javascript自定义九宫格网格验证码插件

所属分类: 网页特效-实用工具    2023-12-16 11:26:22

javascript自定义九宫格网格验证码插件 ie兼容6
 查看演示  登录后下载 温馨提示
登录会员即可享受免费下载
 我要建站

javascript自定义九宫格网格验证码插件(共3个文件)

    • index.html

使用方法

const captchaContainer = document.querySelector("#captchaContainer");
const captchaInput = document.querySelector("#captchaInput");
const mainForm = document.querySelector("#mainForm");
const resultCapcha = document.querySelector("#resultCapcha");
const patternDrowPolyline = document.querySelector("#patternDrowPolyline");
const gridCount = 9;

let result = "";

const createCaptcha = () => {
  var points = "";
  var markCount = 0;
  var captchaItemList = []; 

  for (let i = 0; i < gridCount; i++) {
    var captchaItem = document.createElement("div");
    captchaItem.classList.add("captcha-item");
    // captchaItem.id = `captcha-item-${i}`;

    const randNum = Math.random().toString(20).substr(2, 1);
    
    captchaContainer.appendChild(captchaItem);
    captchaItem.innerHTML = randNum;
  }
    
  const captchaItemId = document.getElementsByClassName("captcha-item");


    while(markCount < 5){
        let selectCaptchaItem = Math.floor(Math.random() * 8);
        
        if(captchaItemList.includes(selectCaptchaItem) == false){
            captchaItemList.push(selectCaptchaItem);
            captchaItemId[selectCaptchaItem].classList.add("marked");
            markCount++;
        }
    }

    captchaItemList.sort();

    captchaItemList.forEach(element => {
        switch (element) {
            case 0:
            points += "40,40,";
            break;
            case 1:
            points += "125,40,";
            break;
            case 2:
            points += "210,40,";
            break;
            case 3:
            points += "40,120,";
            break;
            case 4:
            points += "125,120,";
            break;
            case 5:
            points += "210,120,";
            break;
            case 6:
            points += "40,210,";
            break;
            case 7:
            points += "125,210,";
            break;
            case 8:
            points += "210,210,";
            break;
        }

        result += captchaItemId[element].innerHTML;    
    });
    patternDrowPolyline.innerHTML = ("<polyline points='" + points + "' style='fill:none;stroke:rgb(38, 216, 2);stroke-width:3' />");
    console.log(captchaItemList);
      

    // }
  // alert(points);
//   patternDrowPolyline.innerHTML = ("<polyline points='" + points + "' style='fill:none;stroke:rgb(38, 216, 2);stroke-width:3' />");
};

createCaptcha();

captchaInput.addEventListener("input", () => {
  // alert('val'+captchaInput.value );
  // alert('res'+result );
  if (captchaInput.value != result) {
    captchaInput.classList.add("wrong-captcha");
    resultCapcha.innerHTML = "Wrong Captcha";
    resultCapcha.classList.add("captcha-wrong");
    resultCapcha.classList.remove("captcha-success");
  } else {
    captchaInput.classList.remove("wrong-captcha");
    resultCapcha.classList.remove("captcha-wrong");
    resultCapcha.classList.add("captcha-success");
    resultCapcha.innerHTML = "Captcha Verified";
  }
});

// Get the modal
var modal = document.getElementById("myModal");
// Get the button that opens the modal
var btn = document.getElementById("submit");
// Get the <span> element that closes the modal
var span = document.getElementsByClassName("close")[0];
var message = document.getElementById("message");

btn.onclick = function() {
  if (captchaInput.value != result) {
    modal.style.display = "block";
    message.innerHTML = "Wrong Captcha!! Try again"
    message.style.color = "red";
  } else {
    modal.style.display = "block";
    message.innerHTML = "Captcha Varified"
    message.style.color = "green";
  }
}

span.onclick = function() {
  modal.style.display = "none";
}

// mainForm.addEventListener("submit", (e) => {
//   if (
//     captchaInput.classList.contains("wrong-captcha") ||
//     (!captchaInput.value.length && result.length)
//   ) {
//     e.preventDefault();
//   }
// });

站长提示:
1. 苦力吧素材官方QQ群:950875342
2. 平台上所有素材资源,需注册登录会员方能正常下载。
3. 会员用户积极反馈网站、素材资源BUG或错误问题,每次奖励2K币
4. PHP源码类素材,如需协助安装调试,或你有二次开发需求,可联系苦力吧客服。
5. 付费素材资源,需充值后方能下载,如有任何疑问可直接联系苦力吧客服
相关资源 / 实用工具

MYSQL查询/插入/更新/删除SQL语句在线生成插件

一款数据库增删改查语句自动生成器实用工具插件,包含:Select、INSERT、UPDATE、DELETE、CREATE DATABASE、RENAME DATABASE等。
  实用工具
 2268  0

jquery功能比较丰富的颜色拾取器在线工具

一款可自定义显示拾取器弹出层元素插件,可选择纯色、线性渐变、径向渐变、角度等,对混合颜色进行在线进行选择拾取,非常棒。
  实用工具
 1835  0

javascript订单小费打赏在线计算器工具

一款根据金额计算小费工具,它根据账单金额、服务质量和分摊账单的人数来计算小费金额。
  实用工具
 8239  0

jquery可自定义生成随机密码工具插件

一个简单且自定义的密码生成器插件,可配置的选项:纯数字、小写字母、大写字母、特殊字符、字符位数。点击密码即可复制到剪贴板。
  实用工具
 1243  0

评论数(0) 回复有机会获得K币 用户协议

^_^ 还没有人评论,快来抢个沙发!
😀
  • 😀
  • 😊
  • 😂
  • 😍
  • 😑
  • 😷
  • 😵
  • 😛
  • 😣
  • 😱
  • 😋
  • 😎
  • 😵
  • 😕
  • 😶
  • 😚
  • 😜
  • 😭
发表评论