<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>验证码</title>
</head>
<script>
var chars=[];
for(var a=48;a<=57;a++){
chars.push(String.fromCharCode(a));
}
for(var b=65;b<=90;b++){
chars.push(String.fromCharCode(b));
}
for(var c=97;c<=122;c++){
chars.push(String.fromCharCode(c));
}
function getCode(){
var codes=[];
while(codes.length<4){
var r=parseInt(Math.random()*62);
codes.push(chars[r]);
}
return codes.join("");
}
var code=getCode();
while((prompt("请输入你的验证码"+"验证码是:"+code)).toUpperCase()!=code.toUpperCase()){
alert("验证码错误");
code=getCode();
}
alert("验证码正确");
</script>
<body>
</body>
</html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>验证码</title>
</head>
<script>
var chars=[];
for(var a=48;a<=57;a++){
chars.push(String.fromCharCode(a));
}
for(var b=65;b<=90;b++){
chars.push(String.fromCharCode(b));
}
for(var c=97;c<=122;c++){
chars.push(String.fromCharCode(c));
}
function getCode(){
var codes=[];
while(codes.length<4){
var r=parseInt(Math.random()*62);
codes.push(chars[r]);
}
return codes.join("");
}
var code=getCode();
while((prompt("请输入你的验证码"+"验证码是:"+code)).toUpperCase()!=code.toUpperCase()){
alert("验证码错误");
code=getCode();
}
alert("验证码正确");
</script>
<body>
</body>
</html>
本文提供了一个使用JavaScript生成并验证四位字符验证码的简单示例。该示例从包含数字和字母的大数组中随机选择字符,确保每次生成的验证码都是唯一的。用户需要输入显示的验证码才能通过验证。

3294

被折叠的 条评论
为什么被折叠?



