-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathhtml-validation-demo.html
More file actions
361 lines (316 loc) · 8.62 KB
/
html-validation-demo.html
File metadata and controls
361 lines (316 loc) · 8.62 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Contact form</title>
<style>
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
body {
font-family: Helvetica, Arial, sans-serif;
background: #f0f2f5;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
padding: 24px;
color: #1a1a2e;
}
.card {
background: #fff;
border-radius: 16px;
box-shadow: 0 4px 24px rgba(0, 0, 0, 0.08);
width: 100%;
max-width: 480px;
padding: 40px 36px 36px;
}
.demo-banner {
font-size: 13px;
color: #92400e;
background: #fef3c7;
border: 1px solid #fde68a;
border-radius: 8px;
padding: 10px 14px;
margin-bottom: 24px;
line-height: 1.5;
}
h1 {
font-size: 24px;
font-weight: 600;
margin-bottom: 4px;
}
.subtitle {
font-size: 14px;
color: #6b7280;
margin-bottom: 32px;
}
.field {
position: relative;
margin-bottom: 24px;
}
label {
display: block;
font-size: 13px;
font-weight: 600;
color: #374151;
margin-bottom: 6px;
letter-spacing: 0.01em;
}
.required-star {
color: #ef4444;
margin-left: 2px;
}
input,
textarea,
select {
font-family: Helvetica, Arial, sans-serif;
font-size: 16px;
width: 100%;
padding: 10px 14px;
border: 2px solid #d1d5db;
border-radius: 10px;
background: #fafafa;
color: #1a1a2e;
transition: border-color 0.2s, box-shadow 0.2s, background 0.2s;
outline: none;
}
textarea {
resize: vertical;
min-height: 100px;
}
select {
appearance: none;
background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%236b7280' stroke-width='2' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
background-repeat: no-repeat;
background-position: right 14px center;
padding-right: 38px;
cursor: pointer;
}
input:focus,
textarea:focus,
select:focus {
border-color: #6366f1;
box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
background: #fff;
}
/* Validation states — only show after user interaction via :not(:placeholder-shown) or :not(:focus) */
input:not(:placeholder-shown):not(:focus):valid,
textarea:not(:placeholder-shown):not(:focus):valid,
select:valid:not(:focus) {
border-color: #22c55e;
background: #f0fdf4;
}
input:not(:placeholder-shown):not(:focus):invalid,
textarea:not(:placeholder-shown):not(:focus):invalid {
border-color: #ef4444;
background: #fef2f2;
}
/* Validation icons */
.field::after {
position: absolute;
right: 14px;
font-size: 16px;
pointer-events: none;
opacity: 0;
transition: opacity 0.2s;
}
/* Checkmark for valid */
.field:has(input:not(:placeholder-shown):not(:focus):valid)::after,
.field:has(textarea:not(:placeholder-shown):not(:focus):valid)::after {
content: "✓";
color: #22c55e;
opacity: 1;
bottom: 13px;
}
/* X mark for invalid */
.field:has(input:not(:placeholder-shown):not(:focus):invalid)::after,
.field:has(textarea:not(:placeholder-shown):not(:focus):invalid)::after {
content: "✗";
color: #ef4444;
opacity: 1;
bottom: 13px;
}
/* For textarea, position the icon higher */
.field:has(textarea)::after {
top: 40px;
bottom: auto;
}
/* Hint text */
.hint {
display: block;
font-size: 12px;
color: #9ca3af;
margin-top: 5px;
transition: color 0.2s;
}
.field:has(input:not(:placeholder-shown):not(:focus):invalid) .hint,
.field:has(textarea:not(:placeholder-shown):not(:focus):invalid) .hint {
color: #ef4444;
}
/* Row layout for name fields */
.row {
display: flex;
gap: 16px;
}
.row .field {
flex: 1;
}
/* Submit button */
button[type="submit"] {
font-family: Helvetica, Arial, sans-serif;
font-size: 16px;
font-weight: 600;
width: 100%;
padding: 13px 20px;
border: none;
border-radius: 10px;
background: #6366f1;
color: #fff;
cursor: pointer;
transition: background 0.2s, transform 0.1s, box-shadow 0.2s;
margin-top: 8px;
letter-spacing: 0.01em;
}
button[type="submit"]:hover {
background: #4f46e5;
box-shadow: 0 4px 12px rgba(99, 102, 241, 0.35);
}
button[type="submit"]:active {
transform: scale(0.98);
}
/* Footnote */
.footnote {
text-align: center;
font-size: 12px;
color: #9ca3af;
margin-top: 16px;
}
.footnote span {
color: #ef4444;
}
.view-source {
width: 100%;
max-width: 480px;
margin-top: 16px;
}
.view-source summary {
font-size: 13px;
font-weight: 600;
color: #6b7280;
cursor: pointer;
padding: 8px 0;
user-select: none;
}
.view-source summary:hover {
color: #374151;
}
.view-source pre {
background: #1e1e2e;
color: #cdd6f4;
font-family: "SF Mono", Menlo, Consolas, monospace;
font-size: 12px;
line-height: 1.6;
padding: 20px;
border-radius: 12px;
overflow-x: auto;
white-space: pre-wrap;
word-break: break-word;
margin-top: 8px;
}
</style>
</head>
<body>
<form class="card" action="#demo">
<div class="demo-banner">
<strong>Demo:</strong> This form showcases pure HTML & CSS validation with no JavaScript. It does not submit anywhere.
</div>
<h1>Get in touch</h1>
<p class="subtitle">We'd love to hear from you. Fill out the form below.</p>
<div class="row">
<div class="field">
<label for="first">First name<span class="required-star">*</span></label>
<input
type="text"
id="first"
placeholder=" "
required
minlength="2"
pattern="[A-Za-zÀ-ÖØ-öø-ÿ\s\-']+"
>
<span class="hint">At least 2 characters, letters only</span>
</div>
<div class="field">
<label for="last">Last name<span class="required-star">*</span></label>
<input
type="text"
id="last"
placeholder=" "
required
minlength="2"
pattern="[A-Za-zÀ-ÖØ-öø-ÿ\s\-']+"
>
<span class="hint">At least 2 characters, letters only</span>
</div>
</div>
<div class="field">
<label for="email">Email address<span class="required-star">*</span></label>
<input
type="email"
id="email"
placeholder=" "
required
>
<span class="hint">Enter a valid email, e.g. you@example.com</span>
</div>
<div class="field">
<label for="phone">Phone number</label>
<input
type="tel"
id="phone"
placeholder=" "
pattern="[\d\s\+\-\(\)]{7,20}"
>
<span class="hint">Optional — 7–20 digits, may include +, -, ( )</span>
</div>
<div class="field">
<label for="subject">Subject<span class="required-star">*</span></label>
<select id="subject" required>
<option value="" disabled selected>Choose a topic…</option>
<option value="general">General inquiry</option>
<option value="support">Support request</option>
<option value="feedback">Feedback</option>
<option value="partnership">Partnership</option>
</select>
</div>
<div class="field">
<label for="message">Message<span class="required-star">*</span></label>
<textarea
id="message"
placeholder=" "
required
minlength="10"
maxlength="1000"
></textarea>
<span class="hint">Between 10 and 1,000 characters</span>
</div>
<button type="submit">Send message</button>
<p class="footnote"><span>*</span> Required fields</p>
</form>
<details class="view-source">
<summary>View source</summary>
<pre><code id="source-output"></code></pre>
</details>
<script type="module">
const code = document.getElementById('source-output');
const clone = document.documentElement.cloneNode(true);
clone.querySelector('.view-source').remove();
clone.querySelector('script').remove();
code.textContent = '<!DOCTYPE html>\n' + clone.outerHTML.trim();
</script>
</body>
</html>