如何用js生成简单验证码,并验证是否正确的方法
1、html页面如下
<div>
<table border="0" cellspacing="5" cellpadding="5" >
<tr>
<td> <div id="checkCode" class="code" onclick="createCode(4)" ></div></td>
<td> <span onclick="createCode(4)">看不清换一张</span></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" id="inputCode" style="float:left;" /></td>
</tr>
<tr>
<td></td>
<td><input type="button" onclick="validateCode()" value="确定" /></td>
</tr>
</table>
</div>
2、js脚本如下
//页面加载时,生成随机验证码
window.onload=function(){
createCode(4);
} //生成验证码的方法
function createCode(length) {
var code = "";
var codeLength = parseInt(length); //验证码的长度
var checkCode = document.getElementById("checkCode");
////所有候选组成验证码的字符,当然也可以用中文的
var codeChars = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
//循环组成验证码的字符串
for (var i = 0; i < codeLength; i++)
{
//获取随机验证码下标
var charNum = Math.floor(Math.random() * 62);
//组合成指定字符验证码
code += codeChars[charNum];
}
if (checkCode)
{
//为验证码区域添加样式名
checkCode.className = "code";
//将生成验证码赋值到显示区
checkCode.innerHTML = code;
}
} //检查验证码是否正确
function validateCode()
{
//获取显示区生成的验证码
var checkCode = document.getElementById("checkCode").innerHTML;
//获取输入的验证码
var inputCode = document.getElementById("inputCode").value;
//console.log(checkCode);
//console.log(inputCode);
if (inputCode.length <= 0)
{
alert("请输入验证码!");
}
else if (inputCode.toUpperCase() != checkCode.toUpperCase())
{
alert("验证码输入有误!");
createCode(4);
}
else
{
alert("验证码正确!");
}
}
3、验证码效果图如下:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXgAAACbCAIAAADnWBdpAAAOj0lEQVR4nO3dz0tb6R7H8fM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AUQMjo6+n/nGB0ddfoCABb4AggZGRnZkuT34UBg+HfLdZS//VN2j1sjIyNOXwDAAl8AIU+ePNnk8dtQc/kPVTUP/bb54/fU365qHvqNu93mj98ryvc/8pcZePLkidMXALDAF0DI8PDwhpjJ+81DSxsbG0tDzdoL7U3l/iS5jsVfxSxvaWB4eNjpCwBY4AsgZHh4eF3Iq/v3X62vr7+6rzQ3NxtFcP+VtlJzdEm4i6Vos8li+OIEAl8AIUNDQ2siJkKaFy4P/ro2EVJCE2tra78OXg5NrJEvvoShoSGnLwBggS+AkKGhoSyfXwYuK5cHfsmOf6f9NLYvvhvX1rLi8sAvgmPAFycQ+AIIGRwcXOXxc/8lRVEu9f+8+vKe9lO591J7/97LVfIFi76mDIODg05fAMACXwAhg4ODKxx+ily6F4lcuhT5aeXlvfJPbcHLFfLFysrKSmnxysrKy3sKscAc+OIEAl8AIQMDA8sC3vT5/X1vll/c9fe9edPnN/Yz7r7QV31xt/zLi7v0Wv6+N6IjLA8MDDh9AQALfAGE9Pf3/1fAa7XoCL/6XPUrd54X32Re6Cvqvz2/U3ppSX9/v9MXALDAF0BIJBLJCHit+v3q6+Ir5c5z/U3tFfmiaBTFr77OZJ7f0de1JhKJOH0BAAt8AYT09fX9YcUPHUrHD8XXC73fdPT2fqMoivJN78Iff/zQof1bXMYfICltzdLX1+f0BQAs8AUQoqrqW+dQVdXpCwBY4AsgpLe39z/O0dvb6/QFACzwBRDS29v7b+eAL04g8AUQoqrqv5wD/ZETCHwBhCwsLKiq2uMEqqouLCw4fQEAC3wBAJAFvgAAyAJfAABkgS8AALLAFwAAWeALAIAs8AUAQBb4AgAgC3wBAJDlT0KmWxO0FVb7AAAAAElFTkSuQmCC" alt="" width="357" height="134" />
注:createCode可以传递参数,决定生成验证码的位数
4、整体demo源码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>js简单验证码使用</title>
<style>
.code
{
font-family:Arial;
font-style:italic;
color:blue;
font-size:30px;
border:0;
padding:2px 3px;
letter-spacing:3px;
font-weight:bolder;
float:left;
cursor:pointer;
width:150px;
height:50px;
line-height:60px;
text-align:center;
vertical-align:middle;
background-color:#D8B7E3;
}
span {
text-decoration:none;
font-size:12px;
color:#288bc4;
padding-left:10px;
} span:hover {
text-decoration:underline;
cursor:pointer;
} </style>
</head>
<script>
//页面加载时,生成随机验证码
window.onload=function(){
createCode(4);
} //生成验证码的方法
function createCode(length) {
var code = "";
var codeLength = parseInt(length); //验证码的长度
var checkCode = document.getElementById("checkCode");
////所有候选组成验证码的字符,当然也可以用中文的
var codeChars = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
//循环组成验证码的字符串
for (var i = 0; i < codeLength; i++)
{
//获取随机验证码下标
var charNum = Math.floor(Math.random() * 62);
//组合成指定字符验证码
code += codeChars[charNum];
}
if (checkCode)
{
//为验证码区域添加样式名
checkCode.className = "code";
//将生成验证码赋值到显示区
checkCode.innerHTML = code;
}
} //检查验证码是否正确
function validateCode()
{
//获取显示区生成的验证码
var checkCode = document.getElementById("checkCode").innerHTML;
//获取输入的验证码
var inputCode = document.getElementById("inputCode").value;
console.log(checkCode);
console.log(inputCode);
if (inputCode.length <= 0)
{
alert("请输入验证码!");
}
else if (inputCode.toUpperCase() != checkCode.toUpperCase())
{
alert("验证码输入有误!");
createCode(4);
}
else
{
alert("验证码正确!");
}
}
</script>
<body>
<div>
<table border="0" cellspacing="5" cellpadding="5" >
<tr>
<td> <div id="checkCode" class="code" onclick="createCode(4)" ></div></td>
<td> <span onclick="createCode(4)">看不清换一张</span></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" id="inputCode" style="float:left;" /></td>
</tr>
<tr>
<td></td>
<td><input type="button" onclick="validateCode()" value="确定" /></td>
</tr>
</table>
</div>
</body>
</html>