Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of Picture CAPTCHA by JS

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how to achieve the function of picture CAPTCHA in JS". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "JS how to achieve picture CAPTCHA function" it!

1. Html code

Body, html {width: 100%; text-align: center;} # picyzm {width: 100px; height: 40px; display: inline-block; margin: 030px;} # verifyCodeDemo {width: 100%; display: flex; margin-top: 200px; justify-content: center;} # btn {margin: 30px auto; background-color: blue; color: # fff; border-radius: 5px; border: 0; width: 100px Height: 40px;}

/ / initialization verification code var verifyCode = new GVerify ({id: "picyzm", type: "blend"}); / / Click the button to verify that var code = document.getElementById ("code_input"); var btn = document.getElementById ("btn"); btn.onclick = function () {var res = verifyCode.validate (code.value) If (res) {alert ("verification passed");} else {alert ("CAPTCHA error");}}

two。 Introduction of gVerify.js

! (function (window, document) {function GVerify (options) {/ / create a graphic CAPTCHA object Receive options object as parameter this.options = {/ / default options parameter value id: ", / / Container Id canvasId:" verifyCanvas ", / / ID width of / / canvas:" 100 ", / / default canvas width height:" 30 ", / / default canvas height type:" blend " / / default type of graphic verification code blend: mixed alphanumeric type, number: pure number, letter: pure letter code: ""} if (Object.prototype.toString.call (options) = = "[object Object]") {/ / determine the incoming parameter type for (var i in options) {/ / based on the passed parameter Modify the default parameter value this.options [I] = options [I] }} else {this.options.id = options;} this.options.numArr = "0 1, 2, 3, 4, 5, 6, 7, 8, 9" .split (","); this.options.letterArr = getAllLetter (); this._init (); this.refresh () } GVerify.prototype = {/ * * version number * * / version: '1.0.0, / * initialization method * * / _ init: function () {var con = document.getElementById (this.options.id); var canvas = document.createElement ("canvas"); this.options.width = con.offsetWidth > 0? Con.offsetWidth: "100s"; this.options.height = con.offsetHeight > 0? Con.offsetHeight: "30"; canvas.id = this.options.canvasId; canvas.width = this.options.width; canvas.height = this.options.height; canvas.style.cursor = "pointer"; canvas [XSS _ clean] = "your browser version does not support canvas"; con.appendChild (canvas); var parent = this Canvas.onclick = function () {parent.refresh ()}}, / * * generate CAPTCHA * * / refresh: function () {this.options.code = ""; var canvas = document.getElementById (this.options.canvasId) If (canvas.getContext) {var ctx = canvas.getContext ('2d');} else {return;} ctx.textBaseline = "middle"; ctx.fillStyle = randomColor (180,240); ctx.fillRect (0,0, this.options.width, this.options.height) If (this.options.type = = "blend") {/ / determine the CAPTCHA type var txtArr = this.options.numArr.concat (this.options.letterArr);} else if (this.options.type = = "number") {var txtArr = this.options.numArr;} else {var txtArr = this.options.letterArr } for (var I = 1; I

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report