forked from jackfrued/Python-100-Days
-
Notifications
You must be signed in to change notification settings - Fork 35
/
form.html
177 lines (177 loc) · 4.91 KB
/
form.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style>
* {
margin: 0;
padding: 0;
font-size: 18px;
}
#login label {
display: inline-block;
width: 150px;
text-align: right;
margin-right: 20px;
}
.formitem {
margin: 20px 0;
}
.hint {
display: inline-block;
width: 320px;
font-size: 14px;
margin-left: 10px;
}
.correct {
color: green;
}
.incorrect {
color: red;
}
#login input[type="submit"] {
display: inline-block;
width: 120px;
height: 30px;
background-color: darkred;
color: white;
font-size: 20px;
line-height: 30px;
border: none;
cursor: pointer;
margin-left: 200px;
}
</style>
</head>
<body>
<form id="login" action="" method="post">
<div class="formitem">
<label for="username">用户名: </label>
<input type="text" id="username" name="username">
<span id="uidHint" class="hint"></span>
</div>
<div class="formitem">
<label for="password">密码: </label>
<input type="password" id="password" name="password">
<span id="pwdHint" class="hint"></span>
</div>
<div class="formitem">
<label for="repassword">确认密码: </label>
<input type="password" id="repassword">
<span id="rePwdHint" class="hint"></span>
</div>
<div class="formitem">
<label for="tel">手机号: </label>
<input type="text" id="tel" name="tel">
<span id="telHint" class="hint"></span>
</div>
<div class="formitem">
<label for="code">验证码: </label>
<input type="text" id="code" name="code" size="4">
<input type="button" value="获取验证码">
</div>
<div class="formitem">
<input type="submit" value="立即开通">
</div>
<div class="formitem">
<label for="agreement"></label>
<input type="checkbox" id="agreement">
<span class="hint">我同意<a href="#">《XYZ服务协议》</a></span>
</div>
</form>
<script src="js/mylib.js" ></script>
<script>
(function() {
// 使用正则表达式的字面量语法创建正则表达式对象
var uidRegEx = /^\w{6,20}$/;
var pwdRegEx = /^.{8,20}$/;
var telRegEx = /^1[345789]\d{9}$/;
var uid = $('username');
function checkUsername() {
var uidHint = $('uidHint');
var username = uid.value.trim();
if (uidRegEx.test(username)) {
uidHint.textContent = '√';
uidHint.className = 'hint correct';
return true;
} else {
uidHint.textContent = '用户名由字母数字下划线构成且长度为6-20个字符';
uidHint.className = 'hint incorrect';
return false;
}
}
handleEvent(uid, 'blur', checkUsername);
var pwd = $('password');
function checkPassword() {
var pwdHint = $('pwdHint');
var password = pwd.value;
if (pwdRegEx.test(password)) {
pwdHint.textContent = '√';
pwdHint.className = 'hint correct';
return true;
} else {
pwdHint.textContent = '密码长度为8-20个字符';
pwdHint.className = 'hint incorrect';
return false;
}
}
handleEvent(pwd, 'blur', checkPassword);
var rePwd = $('repassword');
function checkRepassword() {
var rePwdHint = $('rePwdHint');
var password = pwd.value;
var repassword = rePwd.value;
if (repassword.length == 0) {
rePwdHint.textContent = '确认密码不能为空';
rePwdHint.className = 'hint incorrect';
return false;
}
if (repassword == password) {
rePwdHint.textContent = '√';
rePwdHint.className = 'hint correct';
return true;
} else {
rePwdHint.textContent = '密码和确认密码不一致';
rePwdHint.className = 'hint incorrect';
return false;
}
}
handleEvent(rePwd, 'blur', checkRepassword);
var telInput = $('tel');
function checkTel() {
var telHint = $('telHint');
var tel = telInput.value;
if (telRegEx.test(tel)) {
telHint.textContent = '√';
telHint.className = 'hint correct';
return true;
} else {
telHint.textContent = '请输入有效的手机号';
telHint.className = 'hint incorrect';
return false;
}
}
handleEvent(telInput, 'blur', checkTel);
var form = $('login') || document.forms[0];
// 给表单对象绑定表单提交事件
handleEvent(form, 'submit', function(evt) {
evt = evt || window.event;
// 阻止表单提交等到验证通过了之后手动提交表单
evt.preventDefault();
if (!$('agreement').checked) {
alert('请先选中同意《XYZ服务协议》');
return ;
}
// 请注意&&和&之间区别 前者有短路效果后者没有
if (checkUsername() & checkPassword() &
checkRepassword() & checkTel()) {
var target = evt.target || evt.srcElement;
// 如果所有表单数据验证都通过了就提交表单
target.submit();
}
});
}());
</script>
</body>
</html>