随机生成验证码
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>验证码</title>
</head>
<style type="text/css">
body{text-align: center;}
.code
{
font-family: Arial;
font-style: italic;
color: red;
border: 0;
padding: 2px 10px;
letter-spacing: 2px;
font-weight: bolder;
}
</style>
<body>
<input type="text" id="input1" />
<input type="text" onclick="createCode()" id="checkCode" style="width: 45px;background: #660099"/>
<script type="text/javascript" src="https://qunarzz.com/jquery/prd/jquery-1.7.2.js"></script>
<script language="javascript" type="text/javascript">
var code;
function createCode() {
code = "";
var checkCode = document.getElementById("checkCode");
function RndNum() {
var rnd = "";
for (var i = 0; i < 2; i++)
rnd += Math.floor(Math.random() *10)+String.fromCharCode(Math.floor( Math.random() * 26)+ "A".charCodeAt());
return rnd;
}
var num = RndNum();
code = num;
if (checkCode) {
checkCode.className = "code";
checkCode.value = code;
}
}
</script>
<script type="text/javascript">
$(document).ready(function () {
createCode();
$("#input1").blur(function () {
var inputCode = document.getElementById("input1").value;
if (inputCode.length <= 0) {
alert("请输入验证码!");
}
else if (inputCode != code) {
alert("验证码输入错误!");
createCode(); //刷新验证码
}
else {
alert("^-^, ok! !");
}
});
})
</script>
</body>
</html>