最近找了一个比较好看的验证码(真的就是画出了一个验证码) 修改了一下并且加了一些验证和效果 发给大家参考,用了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 -->