验证码

来源:互联网 发布:mac银河雪山 编辑:程序博客网 时间:2024/06/05 04:07
<script type="text/javascript">
var auth=[];
  for(var i=48; i <= 57; i++){
auth.push(String.fromCharCode(i));
}
  for (var a=65; a <= 90; a++){
auth.push(String.fromCharCode(a));//push 
}
   for (var b=97; b <= 122; b++){
auth.push(String.fromCharCode(b));//String.fromCharCode(b):从字符编码创建一个字符串。
}  
function Code(){
var Codes=[];
while(Codes.length<4){
var c=parseInt(Math.random()*62);//parseInt把小数变为整数.Math.random:生成随机数
            Codes.push(auth[c]);
}
return Codes.join("");
}                                        
var Codes1=Code();
while((prompt("请输入你的验证码"+Codes1)).toUpperCase()!=Codes1.toUpperCase()){//toUpperCase()把字符串转换为大写。
     alert("验证码错误");
     Codes1=Code();
}
   alert("验证码正确");
</script>