forked from lijinke666/javascript-demos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path验证码.html
93 lines (91 loc) · 2.83 KB
/
验证码.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>js验证码</title>
<style>
body,input,button{
margin: 0;
padding: 0;
font-family: "Microsoft YaHei";
text-align: center;
}
input{
border:1px solid #e3e3e3;
background: none;;
height: 45px;
outline: none;
text-align: left;
padding-left: 10px;
}
button{
background: none;
border:none;
background: #e3e3e3;
width: 200px;
height: 40px;
cursor: pointer;
outline: none;
display: inline-block;
}
.codeBox{
display: inline-block;
margin: 0 10px;
width: 200px;
height: 200px;
border-radius: 50%;
background: #396;
vertical-align: sub;
font-size: 18px;
text-align: center;
color:#FFF;
letter-spacing: 5px;
font-style: italic;
font-family: "Comic Sans MS";
line-height: 200px;
cursor: pointer;
}
</style>
</head>
<body>
<fieldset>
<span>验证码:</span><span class="codeBox"></span><input type="text" placeholder="请输入验证码">
<p><button id="send">确定</button></p>
</fieldset>
</body>
<script>
window.onload=function(){
random();
var send = document.querySelector("#send");
send.onclick=function(){
var code= document.querySelector(".codeBox").innerHTML;
var inputVal= document.querySelector("input");
if(inputVal.value.toLocaleLowerCase()===code.toLocaleLowerCase()){ //全部转换为小写
alert("正确");
inputVal.value="";
random();
}else{
alert("错误");
inputVal.value="";
random();
}
}
document.querySelector(".codeBox").onclick=function(){
random();
}
};
function random(){
var code="";
var codeLength=6; //验证码长度
var array=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
for(var i=0; i<codeLength;i++){
var arrayNum = Math.floor(Math.random()*array.length); //随机下标
code+=array[arrayNum]; //拼成一个字符串
}
var codeBox = document.querySelector(".codeBox");
codeBox.innerHTML=code;
}
</script>
</html>