JavaScript编写简洁验证码
HTML部分
<HTML>
<head>
<meta charset="UTF-8">
<title><title>
<style type="text/css">
.code{
font-family:Arial;
font-weight: bold;
border: 0;
letter-spacing: 3px;
color: blue;
}
</style>
<script type="txet/JavaScript" src="附件.js"></script>
</head>
<body>
<div>
<input type="text" id="input" />
<input type="button" id="checkCode" class="code" οnclick="createCode()" />
<input type="button" value="验证" οnclick="validate()" />
</div>
</body>
JavaScript部分
var code;
window.onload = functiion(){
createCode();
}
function createCode(){
codr = "";
var codeLength = 6;
var checkCode = document.getElementById('checkCode');
var strArr = [0,1,2,3,4,5,6,7,8,9];
for (var j = 0; j < 26; j++){
strArr.push(String.fromCharCode(65+j));
}
for(var i = 0; i < 6; i++) {
var charIndex = Math.floor(Math.random() * strArr.length);
code += strArr[charIndex];
}
checkCode.value = code;
function validate() {
var inputCode = document.getElementById("input").value.toUpperCase();
if(inputCode.length <= 0) {
alert("请输入验证码!");
}
else if(inputCode != code) {
alert("验证码输入错误!");
createCode();
}
else {
alert("正确");
}
}