-
Notifications
You must be signed in to change notification settings - Fork 294
/
index.html
120 lines (107 loc) · 3.38 KB
/
index.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>原生JS实现随机生成字母数字验证码</title>
<style>
fieldset {
width: 400px;
height: 100px;
padding: 20px;
}
.validateCode {
height: 25px;
padding-left: 5px;
margin-right: 10px;
}
.code {
color: red;
font-style: italic;
display: inline-block;
height: 30px;
width: 65px;
padding: 0 5px;
line-height: 27px;
letter-spacing: 4px; /*调整字母间距*/
background-image: url('./img/timg.jpg');
}
.refresh {
cursor: pointer;
display: inline-block;
vertical-align: middle;
}
.wrong-show {
font-size: 12px;
line-height: 12px;
color: red;
}
.btn {
width: 70px;
height: 30px;
border-radius: 4px;
outline: none;
border: none;
background-color: #287599;
color: #fff;
}
</style>
</head>
<body>
<fieldset>
<legend>原生JS实现随机生成字母数字验证码</legend>
<form action="#">
<label for="codes">
<input id="codes" class="validateCode">
</label>
<span class="code"></span>
<span class="refresh"><img src="./img/refresh.png"></span>
<br/>
<span class="wrong-show"></span>
<br>
<button class="btn" type="button">确定</button>
</form>
</fieldset>
<script>
var valiCode = document.getElementsByClassName('validateCode')[0];
var code = document.getElementsByClassName('code')[0];
var wrongShow = document.getElementsByClassName('wrong-show')[0];
var refresh = document.getElementsByClassName('refresh')[0];
var btn = document.getElementsByClassName('btn')[0];
var result = [];
/*声明一个数组包含所有字母及数字*/
var arr = ['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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
/*生成一个四位随机字符串*/
function getRandomStr() {
result.lenght = 0;
for (var i = 0; i < 4; i++) {
var num = Math.floor(Math.random() * 62);
result[i] = arr[num];
}
var codeStr = result.join('');
code.innerText = codeStr;
}
/*初始化一个验证码*/
window.onload = getRandomStr();
/*点击页面中该字符串重新生成一次*/
refresh.onclick = function () {
getRandomStr();
};
/*点击确定按钮进行验证并显示结果*/
btn.onclick = function () {
var val = valiCode.value;
var current = result.join('');
console.log(val, typeof val, current, typeof current)
if (current == val) {
/*wrongShow.innerText = '验证码输入正确!';*/
getRandomStr();
alert('验证码输入正确!');
} else {
wrongShow.innerText = '验证码输入有误!';
getRandomStr();
}
};
</script>
</body>
</html>