Home  >  Article  >  Web Front-end  >  How to type the verification code in javascript

How to type the verification code in javascript

WBOY
WBOYOriginal
2023-05-21 13:03:081048browse

The code to verify the verification code in JavaScript refers to the function of using JavaScript language to implement verification code verification in the web page. Verification code is a security protection mechanism that mainly displays some random and complex characters/numbers on web pages through graphics, text, etc., and requires users to enter correct information according to prompts to prove their identity or prevent malicious operations of the machine.

Generally speaking, the code for verifying the verification code on the web page can be divided into two parts: generating the verification code and verifying the verification code.

1. Generate verification code

JavaScript can be used to generate verification code using Canvas or DOM elements.

  1. Use Canvas
//生成随机字符串
function createCode() {
  var code = "";
  var codeLength = 4; //验证码长度
  var charList = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  for (var i = 0; i < codeLength; i++) {
    var randomCharIndex = Math.floor(Math.random() * charList.length);
    code += charList[randomCharIndex];
  }
  return code;
}

//设置背景颜色
function drawBackground(ctx) {
  ctx.fillStyle = "#eee"; //背景颜色
  ctx.fillRect(0, 0, 80, 28); //画出矩形背景
}

//画出随机字符串
function drawCode(ctx, code) {
  ctx.fillStyle = "#000"; //字符串颜色
  ctx.font = "24px Arial"; //字体大小和字体
  ctx.fillText(code, 10, 22); //绘制文字
}

//生成验证码
function createCheckCode() {
  var canvas = document.getElementById("checkCodeCanvas");
  var ctx = canvas.getContext("2d");
  drawBackground(ctx);
  var code = createCode();
  drawCode(ctx, code);
  return code; //将生成的验证码返回
}
  1. Use DOM elements
//生成随机字符串
function createCode() {
  var code = "";
  var codeLength = 4; //验证码长度
  var charList = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  for (var i = 0; i < codeLength; i++) {
    var randomCharIndex = Math.floor(Math.random() * charList.length);
    code += charList[randomCharIndex];
  }
  return code;
}

//生成验证码图片
function createCheckCode() {
  var code = createCode();
  var checkCodeImg = document.getElementById("checkCodeImg");
  checkCodeImg.src = "checkCode.php?code=" + code;
  return code; //将生成的验证码返回
}

2. Verification Verification Code

Verification Verification The code is mainly implemented by judging whether the value entered by the user in the input box is consistent with the generated random string.

function validateCheckCode() {
  var inputCode = document.getElementById("inputCode").value.trim().toLowerCase();
  var checkCode = document.getElementById("checkCode").value.toLowerCase(); //checkCode是之前生成的随机字符串
  if (inputCode.length <= 0) {
    alert("请输入验证码!");
    return false;
  } else if (inputCode != checkCode) {
    alert("验证码错误!请重新输入!");
    createCheckCode(); //生成新的验证码
    document.getElementById("inputCode").value = ""; //清空输入框
    return false;
  } else {
    alert("验证码正确!");
    return true;
  }
}

The above is the code to generate and verify the verification code in JavaScript. It should be noted that although the verification code can prevent malicious access caused by the machine to a certain extent, it will also bring about a certain degree of user experience problems, so you need to weigh the pros and cons when using it.

The above is the detailed content of How to type the verification code in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn