2种算法
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>验证码</title>
<style type="text/css">
div{
background:rgb(219, 192, 255);
height: 30px;
width: 60px;
text-align: center;
line-height: 30px;
}
</style>
</head>
<body>
<div id="box"></div>
<div id="box1"></div>
<script>
//全部列出来,再遍历
var oBox = document.getElementById("box");
var str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz";
var code = "";
for(var i = 0; i <4;i++){
var randIndex = parseInt(Math.floor(Math.random()*str.length));
var randCode = str[randIndex];
code += randCode;
}
oBox.innerHTML = code;
// 利用ASCII值
var oBox1 = document.getElementById("box1");
var str1 = "";
while(str1.length<4){
var code1 = Math.floor(Math.random()*75+48)
if(code1>=48 && code1<=57 ||code1>=65 && code1<=90 || code1>=97 && code1<=122){
var code2 = String.fromCharCode(code1)
str1 += code2
}
oBox1.innerHTML = str1
}
</script>
</body>
</html>