<span style="font-size:18px;"><!DOCTYPE html>
<head>
<meta charset="utf-8"/>
<title> </title>
</head>
<body>
<script>
var chars=[];//保存所有的字符
//初始化备选字符数组
//填充数字字符
//i从48开始,到<=57结束,每次增1
for(i=48;i<=57;i++){
//将i转为字符,将字符压入chars中
chars.push(String.fromCharCode(i));
}
//填充大写字母
for(i=65;i<=90;i++){
//将i转为字符,将字符压入chars中
chars.push(String.fromCharCode(i));
}
//填充小写字母
for(i=97;i<=122;i++){
//将i转为字符,将字符压入chars中
chars.push(String.fromCharCode(i));
}
console.log(chars.length);//62
console.log(String(chars));
function getCode(){//从新获取验证码
//i从0开始,到<4结束,每次增1,同时声明空字符串code
for(i=0,code="";i<4;i++){
//在0~chars的元素个数-1之间生成一个随机整数r
var r=parseInt(Math.random()*(chars.length));//chars.length-1+0+1
//将chars中的r位置的元素拼接到code上
code+=chars[r];
//(遍历结束)
}
return code;
//返回code
}
console.log(getCode());
var code=getCode();//获得一个验证码
while((input=prompt("输入验证码("+code+")")).toLowerCase()!=code.toLowerCase()){
alert("验证码错误");
code=getCode();//生成新的验证码
}
document.write("<h1 style='color:green'>验证通过</h1>");
</script>
</body>
</html>
</span>
验证码
最新推荐文章于 2025-02-26 20:36:13 发布