javascript绘制的简约验证码

最近找了一个比较好看的验证码(真的就是画出了一个验证码)  修改了一下并且加了一些验证和效果  发给大家参考,用了bs3无用的代码会比较多,请自行忽略

html部分

<div class="form-group">
    <label class="control-label">验证码</label><span style="color:red ">(若输入完毕,请您点击_空白处_验证)</span>
    <div class="input-group">
        <input style="width: 240px" type="text" class="topAlign form-control" id="verify" name="verify" required>
        <span class="glyphicon glyphicon-ok" id="glyphicon-ok" style="margin-left: 5px; margin-top: 5px ;font-size: 20px ;display: none"></span>
        <span class="glyphicon glyphicon-remove" id="glyphicon-remove" style="margin-left: 5px; margin-top: 5px ;font-size: 20px ;display: none"></span>
    </div>
    <canvas width="100" height="40" id="verifyCanvas"></canvas>
    <img id="code_img">
</div>
<div class="form-group">
    <a id="submitbtn"  class="btn btn-default btn-lg" style="margin-left: 100px; display: none" href="">提交</a>
</div>

js部分

<!-- 验证码start -->
<script type="text/javascript">
    var nums = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];
    var str="";//验证码字串
    drawCode();
    function drawCode() {
        var canvas = document.getElementById("verifyCanvas");  
        var context = canvas.getContext("2d");                
        context.fillStyle = "cornflowerblue";                  
        context.fillRect(0, 0, canvas.width, canvas.height);   
        context.fillStyle = "white";                           
        context.font = "25px Arial";                           
        var rand = new Array();
        var x = new Array();
        var y = new Array();
        for (var i = 0; i < 5; i++) {
            rand[i] = nums[Math.floor(Math.random() * nums.length)]
            x[i] = i * 16 + 10;
            y[i] = Math.random() * 20 + 20;
            context.fillText(rand[i], x[i], y[i]);
            str=str+rand[i];
        }
        for (var i = 0; i < 3; i++) {
            drawline(canvas, context);
        }
        for (var i = 0; i < 30; i++) {
            drawDot(canvas, context);
        }
        convertCanvasToImage(canvas)
    }
    function drawline(canvas, context) {
        context.moveTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height));            
        context.lineTo(Math.floor(Math.random() * canvas.width), Math.floor(Math.random() * canvas.height));  
        context.lineWidth = 0.5;                                                  
        context.strokeStyle = 'rgba(50,50,50,0.3)';                               
        context.stroke();                                                 
    }
    function drawDot(canvas, context) {
        var px = Math.floor(Math.random() * canvas.width);
        var py = Math.floor(Math.random() * canvas.height);
        context.moveTo(px, py);
        context.lineTo(px + 1, py + 1);
        context.lineWidth = 0.2;
        context.stroke();
    }
    function convertCanvasToImage(canvas) {
        document.getElementById("verifyCanvas").style.display = "none";
        var image = document.getElementById("code_img");
        image.src = canvas.toDataURL("image/png");
        return image;
    }
    // 刷新
    document.getElementById('code_img').onclick = function () {
        $('#verifyCanvas').remove();
        $('#verify').after('<canvas width="100" height="40" id="verifyCanvas"></canvas>')
        str="";
        drawCode();
        $("#glyphicon-ok").hide();
    	$("#glyphicon-remove").hide();
        $("#submitbtn").hide();
    }
    //判断
    $("#verify").blur(function(){
    	if (str==document.getElementById("verify").value) {
    		//$("#submitbtn").removeClass("disabled");
    		$("#glyphicon-remove").hide();
    		$("#glyphicon-ok").show();
    		$("#submitbtn").show();
      	}else {
      		$("#glyphicon-ok").hide();
    		$("#glyphicon-remove").show();
    		$("#submitbtn").hide();
      	}
    });
    $("#times").click(function(){
    	window.location.reload();
    });
    
</script>
<!-- 验证码end -->


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值