-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathday2005-validated-form.html
293 lines (267 loc) · 8.68 KB
/
day2005-validated-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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<!doctype html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>信息采集页</title>
<style>
body {
background-color: #222;
padding: 20px;
color: #fff;
}
main {
max-width: 750px;
margin: 0 auto;
}
.form {
display: flex;
flex-direction: column;
}
.form > h2 {
position: relative;
text-align: center;
}
.form > h2::after,
.form > h2::before {
content: "";
border-bottom: solid #ff7200 2px;
display: block;
position: absolute;
width: 30%;
left: 0;
top: 50%;
}
.form > h2::after {
right: 0;
left: unset;
}
.form--item {
display: flex;
flex-direction: column;
gap: 6px;
padding-bottom: 8px;
}
.form--feedback {
font-size: .8em;
color: red;
min-height: 1.3em;
transition: .3s;
}
.form--feedback.form--feedback__hidden {
opacity: 0;
}
.form > .form--controls {
display: flex;
gap: 6px;
}
.form > .form--controls > button {
background-color: #666666;
border-radius: 6px;
flex-grow: 1;
color: #fff;
padding: 8px 0;
font-size: 1.2em;
cursor: pointer;
}
.form > .form--controls > button:active {
opacity: .8;
}
.form > .form--controls > button:first-of-type {
background-color: #ff7200;
color: black;
}
.image-upload {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
padding: 24px;
border-radius: 6px;
background-color: #444;
margin-bottom: 12px;
font-size: 24px;
border: dashed #666 4px;
user-select: none;
cursor: pointer;
}
.image-upload > p {
margin-bottom: 0;
}
.image-preview {
display: flex;
flex-direction: column;
gap: 12px;
width: 100%;
padding-top: 6px;
}
.image-preview > img {
width: 100%;
height: auto;
border-radius: 6px;
object-fit: contain;
}
</style>
</head>
<body>
<main>
<h1>信息采集页</h1>
<form action="https://httpbin.org/post" method="post" class="form">
<h2>基础信息</h2>
<div class="form--item">
<label for="account">账号</label>
<input type="text"
autocomplete="username"
name="account"
id="account"
placeholder="此处填写您的账号">
<span class="form--feedback"></span>
</div>
<div class="form--item">
<label for="password">密码</label>
<input type="password"
autocomplete="current-password"
name="password"
id="password"
placeholder="此处填写您的密码">
<span class="form--feedback"></span>
</div>
<div class="form--item">
<label for="telephone">手机号码</label>
<input type="tel"
autocomplete="tel"
name="telephone"
id="telephone"
placeholder="此处填写您的手机号码">
<span class="form--feedback"></span>
</div>
<div class="form--item">
<label for="email">电子邮箱</label>
<input type="email"
autocomplete="email"
name="email"
id="email"
placeholder="此处填写您的电子邮箱">
<span class="form--feedback"></span>
</div>
<div class="form--item">
<label for="birthday">出生年月日</label>
<input type="date"
autocomplete="birthday"
name="birthday"
id="birthday"
placeholder="此处填写您的出生年月日">
<span class="form--feedback"></span>
</div>
<div class="form--item">
<label for="link">个人主页网址</label>
<input type="url"
autocomplete="url"
name="link"
id="link"
placeholder="此处填写您的个人主页网址">
<span class="form--feedback"></span>
</div>
<h2>上传照片</h2>
<section class="image-upload">
<p>点击上传照片或者拖拽照片到此处</p>
<span class="form--feedback form--feedback__hidden">不要添加同样的照片</span>
<div class="image-preview">
</div>
<input type="file"
name="images"
id="images"
hidden
multiple
accept="image/jpeg,image/png,image/webp">
</section>
<div class="form--controls">
<button type="submit">提交</button>
<button type="reset">重置</button>
</div>
</form>
</main>
<script>
const oInputAccount = document.querySelector("#account");
const oInputPassword = document.querySelector("#password");
const oInputTelephone = document.querySelector("#telephone");
const oInputEmail = document.querySelector("#email");
const oInputLink = document.querySelector("#link");
const oImageUpload = document.querySelector(".image-upload");
const oImagePreview = document.querySelector(".image-preview");
const oImageInput = document.querySelector("#images");
const oImageUploadFeedback = oImageUpload.querySelector(".form--feedback");
const state = {
addedImages: new Set()
};
attachValidator(oInputAccount,
[text => /^[a-zA-Z0-9-_]*$/.test(text),
"账号只能包含字母、数字、下划线、短横线"],
[text => {
const len = text.trim().length;
return 6 <= len && len <= 12;
}, "只能是6到12位"]);
attachValidator(oInputPassword,
[text =>
[/[A-Z]/, /[a-z]/, /[0-9]/, /[~`!@#$%^&*()+-\/\\{}\[|;:'"<>.?]/]
.every(reg => reg.test(text)),
"必须包含大写字母、小写字母、数字、特殊字符"],
[text => {
const len = text.trim().length;
return 6 <= len && len <= 20;
}, "长度只能是6到20位"]);
attachValidator(oInputTelephone, [text => /^[0-9]{11}$/.test(text), "手机号码是11位数字"]);
attachValidator(oInputEmail,
[text => /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/.test(text),
"电子邮箱是“用户名@主机名”的形式"]);
attachValidator(oInputLink, [text => /^(((ht|f)tps?):\/\/)?[\w-]+(\.[\w-]+)+([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?$/.test(text), "请填写正确的网址"]);
function attachValidator(input, ...validations) {
const oFeedback = input.closest(".form--item").querySelector(".form--feedback");
oFeedback.classList.add("form--feedback__hidden");
input.addEventListener("input", () => {
let showFeedback = false;
for (const validation of validations) {
const [validator, feedbackText] = validation;
if (!validator(input.value)) {
feedbackText && (oFeedback.textContent = feedbackText);
showFeedback = true;
break;
}
}
if (showFeedback) {
oFeedback.classList.remove("form--feedback__hidden");
} else {
oFeedback.classList.add("form--feedback__hidden");
}
});
}
function addImagePreview(fileList, onDuplicate) {
const newImages = Array.from(fileList).reduce((images, file) => {
if (!state.addedImages.has(file.name)) {
state.addedImages.add(file.name);
const img = document.createElement("img");
img.src = URL.createObjectURL(file);
images.appendChild(img);
} else {
onDuplicate && onDuplicate();
}
return images;
}, document.createDocumentFragment());
oImagePreview.insertBefore(newImages, oImagePreview.firstElementChild);
}
function showImageDuplicateFeedback() {
oImageUploadFeedback.classList.remove("form--feedback__hidden");
setTimeout(() => oImageUploadFeedback.classList.add("form--feedback__hidden"), 4000);
}
oImageUpload.addEventListener("click", () => oImageInput.click());
oImageUpload.addEventListener("drop", e => {
e.preventDefault();
addImagePreview(e.dataTransfer.files, showImageDuplicateFeedback);
});
oImageUpload.addEventListener("dragover", e => e.preventDefault());
oImageInput.addEventListener("change", e => addImagePreview(e.target.files, showImageDuplicateFeedback));
</script>
</body>
</html>