-
Notifications
You must be signed in to change notification settings - Fork 1
/
form-validation.astro
110 lines (95 loc) · 2.24 KB
/
form-validation.astro
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
---
import ThemedCode from "../components/ThemedCode.astro";
import BaseLayout from "../layouts/BaseLayout.astro";
---
<style is:global>
.example p {
margin-bottom: 1rem;
}
fieldset {
padding: 1rem;
}
label {
width: 100%;
margin-right: 1rem;
padding: 1rem;
}
.error {
margin-top: 1.5rem;
display: none;
color: #ff474e;
}
.success {
margin-top: 1.5rem;
display: none;
color: #beff00;
}
label:has(input:user-valid) {
color: #000;
background: #beff00;
}
label:has(input:user-invalid) {
background: #ff474e;
}
fieldset:has(input:user-valid) {
border-left: 10px solid #beff00;
}
fieldset:has(input:user-invalid) {
border-left: 10px solid #ff474e;
}
label:has(input:user-valid) ~ .success,
fieldset:has(input:user-valid) .success {
display: block;
}
label:has(input:user-invalid) ~ .error,
fieldset:has(input:user-invalid) .error {
display: block;
}
</style>
<BaseLayout title="Form Validation">
<div class="description">
<p>
Style a label or fieldset and hide / show an error message based on the
validation status of it's input.
</p>
<p>Try typing a string or negative number into these number inputs (tab to blur the input).</p>
</div>
<ThemedCode lang="css" code=`label:has(input:user-valid) {
color: #000;
background: #beff00;
}
label:has(input:user-invalid) {
background: #ff474e;
}
fieldset:has(input:user-valid) {
border-left: 10px solid #beff00;
}
fieldset:has(input:user-invalid) {
border-left: 10px solid #ff474e;
}
label:has(input:user-valid) ~ .success,
fieldset:has(input:user-valid) .success {
display: block;
}
label:has(input:user-invalid) ~ .error,
fieldset:has(input:user-invalid) .error {
display: block;
}` />
<div class="example">
<p>Label Example:</p>
<label>
Quantity
<input type="number" min="0" value="42" />
</label>
<p class="success">Quantity is valid!</p>
<p class="error">Invalid quantity.</p>
<hr>
<p>Fieldset Example:</p>
<fieldset>
<label>Amount</label>
<input type="number" min="0" value="42" />
<p class="success">Amount is valid!</p>
<p class="error">Invalid amount.</p>
</fieldset>
</div>
</BaseLayout>