-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFormInputValidation.ts
More file actions
198 lines (193 loc) · 6.4 KB
/
FormInputValidation.ts
File metadata and controls
198 lines (193 loc) · 6.4 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
/**
* FormInputValidation: A form with input validation and error handling.
* Demonstrates form.value, validation, and error feedback.
*/
import { component, html, ref, useOnConnected } from '../../lib';
import { when } from '../../lib/directives';
component('form-input-validation', () => {
const email = ref('');
const username = ref('');
const bio = ref('');
const gender = ref('');
const subscribe = ref(false);
const fruits = ref([]);
const country = ref('');
const errorMessage = ref('');
const successMessage = ref('');
const submit = async (event: Event) => {
event.preventDefault();
errorMessage.value = '';
successMessage.value = '';
// Email validation
if (!email.value.match(/^[^@\s]+@[^@\s]+\.[^@\s]+$/)) {
errorMessage.value = 'Please enter a valid email address.';
return;
}
// Username validation
if (!username.value || username.value.length < 3) {
errorMessage.value = 'Username must be at least 3 characters.';
return;
}
// Bio validation
if (!bio.value || bio.value.length < 10) {
errorMessage.value = 'Bio must be at least 10 characters.';
return;
}
// Gender validation
if (!gender.value) {
errorMessage.value = 'Please select a gender.';
return;
}
// Fruits validation (at least one)
if (!Array.isArray(fruits.value) || fruits.value.length === 0) {
errorMessage.value = 'Please select at least one favorite fruit.';
return;
}
// Country validation
if (!country.value) {
errorMessage.value = 'Please select a country.';
return;
}
successMessage.value = 'Form submitted successfully!';
// Reset form fields
email.value = '';
username.value = '';
bio.value = '';
gender.value = '';
subscribe.value = false;
fruits.value = [];
country.value = '';
await setTimeout(() => {
successMessage.value = '';
}, 3000);
};
const emailInput = ref<HTMLElement | null>(null);
useOnConnected(() => {
emailInput.value?.focus();
});
return html`
<form
class="max-w-128 mx-auto p-8 rounded-lg bg-white dark:bg-black text-black dark:text-white shadow-lg border border-neutral-100 dark:border-neutral-900"
@submit="${submit}"
>
<fieldset>
<legend class="text-2xl font-medium mb-8">
Form Input Validation Demo
</legend>
<label class="flex flex-col items-start gap-2 w-full mb-6">
<span class="font-semibold">Email:</span>
<input
:model="${email}"
:ref="${emailInput}"
type="email"
required
class="w-full px-2 py-1 rounded-sm border border-neutral-300 dark:border-neutral-800 hover:bg-neutral-50 focus:bg-white dark:hover:bg-neutral-900 dark:focus:bg-black"
/>
</label>
<label class="flex flex-col items-start gap-2 w-full mb-6">
<span class="font-semibold">Username:</span>
<input
:model="${username}"
type="text"
minlength="3"
required
class="w-full px-2 py-1 rounded-sm border border-neutral-300 dark:border-neutral-800 hover:bg-neutral-50 focus:bg-white dark:hover:bg-neutral-900 dark:focus:bg-black"
/>
</label>
<label class="flex flex-col items-start gap-2 w-full mb-6">
<span class="font-semibold">Bio:</span>
<textarea
:model="${bio}"
rows="3"
minlength="10"
required
class="w-full px-2 py-1 rounded-sm border border-neutral-300 dark:border-neutral-800 hover:bg-neutral-50 focus:bg-white dark:hover:bg-neutral-900 dark:focus:bg-black"
></textarea>
</label>
<div class="flex flex-col items-start gap-2 w-full mb-6">
<span class="font-semibold">Gender:</span>
<label
><input
:model="${gender}"
type="radio"
value="male"
name="gender"
/>
Male</label
>
<label
><input
:model="${gender}"
type="radio"
value="female"
name="gender"
/>
Female</label
>
<label
><input
:model="${gender}"
type="radio"
value="other"
name="gender"
/>
Other</label
>
</div>
<label class="flex flex-col items-start gap-2 w-full mb-6">
<span class="font-semibold">Subscribe:</span>
<div><input :model="${subscribe}" type="checkbox" /> Yes</div>
</label>
<div class="flex flex-col items-start gap-2 w-full mb-6">
<span class="font-semibold">Favorite Fruits:</span>
<label
><input :model="${fruits}" type="checkbox" value="apple" />
Apple</label
>
<label
><input :model="${fruits}" type="checkbox" value="banana" />
Banana</label
>
<label
><input :model="${fruits}" type="checkbox" value="orange" />
Orange</label
>
</div>
<label class="flex flex-col items-start gap-2 w-full mb-6">
<span class="font-semibold">Country:</span>
<select
:model="${country}"
class="w-full px-2 py-1 rounded-sm border border-neutral-300 dark:border-neutral-800 hover:bg-neutral-50 focus:bg-white dark:hover:bg-neutral-900 dark:focus:bg-black"
>
<option value="">Select...</option>
<option value="us">United States</option>
<option value="ca">Canada</option>
<option value="uk">United Kingdom</option>
</select>
</label>
${when(
errorMessage.value !== '',
html`
<div class="mb-6 text-sm text-error-600 dark:text-error-400">
${errorMessage.value}
</div>
`,
)}
<button
type="submit"
class="px-4 py-2 bg-primary-600 text-white rounded-sm hover:bg-primary-500 focus:bg-primary-500"
>
Submit
</button>
${when(
successMessage.value !== '',
html`
<div class="mt-4 text-sm text-success-600 dark:text-success-400">
${successMessage.value}
</div>
`,
)}
</fieldset>
</form>
`;
});