纯前端验证码

 使用:

// 验证码
if(code1.verify(document.getElementById("textfield3").value) === false){
	alert("验证码不正确");
	return;
}		

引入vcode.js

/**
 * Created by Lee on 2014/9/16.
 */
(function(){
    var randstr = function(length){
        var key = {

            str : [
                'a','b','c','d','e','f','g','h','i','j','k','l','m',
                'o','p','q','r','s','t','x','u','v','y','z','w','n',
                '0','1','2','3','4','5','6','7','8','9'
            ],

            randint : function(n,m){
                var c = m-n+1;
                var num = Math.random() * c + n;
                return  Math.floor(num);
            },

            randStr : function(){
                var _this = this;
                var leng = _this.str.length - 1;
                var randkey = _this.randint(0, leng);
                return _this.str[randkey];
            },

            create : function(len){
                var _this = this;
                var l = len || 10;
                var str = '';

                for(var i = 0 ; i<l ; i++){
                    str += _this.randStr();
                }

                return str;
            }

        };

        length = length ? length : 10;

        return key.create(length);
    };

    var randint = function(n,m){
        var c = m-n+1;
        var num = Math.random() * c + n;
        return  Math.floor(num);
    };

    var vCode = function(dom, options){
        this.codeDoms = [];
        this.lineDoms = [];
        this.initOptions(options);
        this.dom = dom;
        this.init();
        this.addEvent();
        this.update();
        this.mask();
    };

    vCode.prototype.init = function(){
    
        this.dom.style.position = "relative";
        this.dom.style.overflow = "hidden";
        this.dom.style.cursor = "pointer";
        this.dom.title = "点击更换验证码";
        this.dom.style.background = this.options.bgColor;
        this.w = this.dom.clientWidth;
        this.h = this.dom.clientHeight;
        this.uW = this.w / this.options.len - 2;
    };

    vCode.prototype.mask = function(){
        var dom = document.createElement("div");
        dom.style.cssText = [
            "width: 100%",
            "height: 100%",
            "left: 0",
            "top: 0",
            "position: absolute",
            "cursor: pointer",
            "z-index: 9999999"
        ].join(";");

        dom.title = "点击更换验证码";

        this.dom.appendChild(dom);
    };

    vCode.prototype.addEvent = function(){
        var _this = this;

        var browser=navigator.appName;
        var b_version=navigator.appVersion;
        var version=b_version.split(";");
        var trim_Version=version[1].replace(/[ ]/g,"");
        if(browser=="Microsoft Internet Explorer" && (trim_Version=="MSIE7.0" || trim_Version=="MSIE8.0"))
        {
//            alert(trim_Version);
            _this.dom.attachEvent("onclick", function(){
                _this.update.call(_this);
            });
        }else{
            _this.dom.addEventListener("click", function(){
                _this.update.call(_this);
            });
        }
    };

    vCode.prototype.initOptions = function(options){

        var f = function(){
            this.len = 4;
            this.fontSizeMin = 16;
            this.fontSizeMax = 32;
            this.colors = [
                "green",
                "red",
                "blue",
                "#53da33",
                "#AA0000",
                "#FFBB00"
            ];
            this.bgColor = "#FFF";
            this.fonts = [
                "Times New Roman",
                "Georgia",
                "Serif",
                "sans-serif",
                "arial",
                "tahoma",
                "Hiragino Sans GB"
            ];
            this.lines = 8;
            this.lineColors = [
                "#888888",
                "#FF7744",
                "#888800",
                "#008888"
            ];

            this.lineHeightMin = 1;
            this.lineHeightMax = 3;
            this.lineWidthMin = 1;
            this.lineWidthMax = 60;
        };

        this.options = new f();

        if(typeof options === "object"){
            for(i in options){
                this.options[i] = options[i];
            }
        }
    };

    vCode.prototype.update = function(){
        for(var i=0; i<this.codeDoms.length; i++){
            this.dom.removeChild(this.codeDoms[i]);
        }
        for(var i=0; i<this.lineDoms.length; i++){
            this.dom.removeChild(this.lineDoms[i]);
        }
        this.createCode();
        this.draw();
    };

    vCode.prototype.createCode = function(){
        this.code = randstr(this.options.len);
    };

    vCode.prototype.verify = function(code){
    	if(this.code !== code ){
    		// 密码错误,刷新验证码
    		 this.update.call(this);
    	}
        return this.code === code;
    };

    vCode.prototype.draw = function(){
        this.codeDoms = [];
        for(var i=0; i<this.code.length; i++){
            this.codeDoms.push(this.drawCode(this.code.charAt(i), i));
        }

//        this.drawLines();
    };

    vCode.prototype.drawCode = function(code, index){
        var dom = document.createElement("span");

        dom.style.cssText = [
                "font-size:" + randint(this.options.fontSizeMin, this.options.fontSizeMax) + "px",
                "color:" + this.options.colors[randint(0,  this.options.colors.length - 1)],
                "position: absolute",
                "left:" + randint(this.uW * index, this.uW * index + this.uW - 10) + "px",
                "top:" + randint(0, this.h - 35) + "px",
                "transform:rotate(" + randint(-30, 30) + "deg)",
                "-ms-transform:rotate(" + randint(-30, 30) + "deg)",
                "-moz-transform:rotate(" + randint(-30, 30) + "deg)",
                "-webkit-transform:rotate(" + randint(-30, 30) + "deg)",
                "-o-transform:rotate(" + randint(-30, 30) + "deg)",
                "font-family:" + this.options.fonts[randint(0, this.options.fonts.length - 1)],
                "font-weight:" + randint(400, 900)
        ].join(";");

        dom.innerHTML = code;
        this.dom.appendChild(dom);

        return dom;
    };

    vCode.prototype.drawLines = function(){
        this.lineDoms = [];
        for(var i=0; i<this.options.lines; i++){
            var dom = document.createElement("div");

            dom.style.cssText = [
                "position: absolute",
                    "opacity: " + randint(3, 8) / 10,
                    "width:" + randint(this.options.lineWidthMin, this.options.lineWidthMax) + "px",
                    "height:" + randint(this.options.lineHeightMin, this.options.lineHeightMax) + "px",
                    "background: " + this.options.lineColors[randint(0, this.options.lineColors.length - 1)],
                    "left:" + randint(0, this.w - 20) + "px",
                    "top:" + randint(0, this.h) + "px",
                    "transform:rotate(" + randint(-30, 30) + "deg)",
                    "-ms-transform:rotate(" + randint(-30, 30) + "deg)",
                    "-moz-transform:rotate(" + randint(-30, 30) + "deg)",
                    "-webkit-transform:rotate(" + randint(-30, 30) + "deg)",
                    "-o-transform:rotate(" + randint(-30, 30) + "deg)",
                    "font-family:" + this.options.fonts[randint(0, this.options.fonts.length - 1)],
                    "font-weight:" + randint(400, 900)
            ].join(";");
            this.dom.appendChild(dom);

            this.lineDoms.push(dom);
        }
    };

    this.vCode = vCode;

}).call(this);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
verify.js是一个前端验证码生成和验证工具。它通过使用JavaScript编写,可以帮助开发者在前端页面中添加验证码功能,以增强网站的安全性。 verify.js的生成验证码的方式多种多样,可以是数字、字母、图像等形式。开发者可以根据自己的需求选择不同的验证码类型,并可以自定义验证码的长度、字体、颜色等样式。生成的验证码可以直接在前端页面中展示给用户,通过verify.js提供的接口,可以轻松地将验证码嵌入到表单、登录页面等需要进行验证的地方。 validate.js也提供了验证验证码的功能,它通过接收用户输入的验证码信息,并与前端生成的验证码进行比对,以确定用户输入的验证码是否正确。验证过程是在前端进行的,可以有效地减轻服务器的负担,提高页面的响应速度。如果用户输入的验证码前端生成的验证码不一致,开发者可以自定义提示信息,提示用户重新输入。只有在用户输入正确的验证码后,才能通过验证码验证,进一步进行后续操作,确保了网站的安全性。 总的来说,verify.js是一个方便易用的前端验证码工具,它提供了生成验证码和验证验证码的功能。通过它,开发者可以简单地在前端页面中添加验证码,并提高网站的安全性。它的使用方法灵活多样,可以满足不同网站的需求。同时,verify.js还支持自定义样式和提示信息,使开发者可以根据自己的需要进行定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值