-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
285 lines (280 loc) · 9.62 KB
/
index.html
File metadata and controls
285 lines (280 loc) · 9.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
crossorigin="anonymous"
/>
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
<title>Hellooo</title>
</head>
<body>
<div
id="app"
class="container d-flex flex-column justify-content-center align-items-center vh-100"
>
<div class="bg-primary btn m-5 px-5 py-2 text-white">
<h3>Welcome</h3>
</div>
<form class="row g-3 needs-validation w-75" @submit="validateForm">
<div class="col-md-4">
<label for="validationCustom01" class="form-label">First name</label>
<input
v-model="formInfo.firstName"
type="text"
class="form-control"
:class="{
'is-invalid': firstNameValid === false,
'is-valid': firstNameValid === true
}"
id="validationCustom01"
@blur="handleFocus('firstName')"
required
/>
<div class="valid-feedback">Looks good!</div>
<div class="invalid-feedback">Please write your First Name.</div>
</div>
<div class="col-md-4">
<label for="validationCustom02" class="form-label">Last name</label>
<input
type="text"
class="form-control"
:class="{
'is-invalid': lastNameValid === false,
'is-valid': lastNameValid === true
}"
id="validationCustom02"
v-model="formInfo.lastName"
@blur="handleFocus('lastName')"
required
/>
<div class="valid-feedback">Looks good!</div>
<div class="invalid-feedback">Please write your Last Name.</div>
</div>
<div class="col-md-4">
<label for="validationCustomUsername" class="form-label"
>Username</label
>
<div class="input-group has-validation">
<span class="input-group-text" id="inputGroupPrepend">@</span>
<input
type="text"
class="form-control"
:class="{
'is-invalid': userNameValid === false,
'is-valid': userNameValid === true
}"
id="validationCustomUsername"
aria-describedby="inputGroupPrepend"
v-model="formInfo.username"
@blur="handleFocus('username')"
required
/>
<div class="valid-feedback">Looks good!</div>
<div class="invalid-feedback">Please choose a username.</div>
</div>
</div>
<div class="col-md-6">
<label for="validationCustom03" class="form-label">City</label>
<input
type="text"
class="form-control"
:class="{
'is-invalid': cityValid === false,
'is-valid': cityValid === true
}"
v-model="formInfo.city"
@blur="handleFocus('city')"
id="validationCustom03"
required
/>
<div class="invalid-feedback">Please provide a valid city.</div>
</div>
<div class="col-md-3">
<label for="validationCustom04" class="form-label">State</label>
<select
class="form-select"
:class="{
'is-invalid': stateValid === false,
'is-valid': stateValid === true
}"
v-model="formInfo.state"
id="validationCustom04"
@blur="handleFocus('state')"
required
>
<option selected disabled value="">Choose...</option>
<option>Dhaka</option>
<option>Barisal</option>
<option>Citagaon</option>
<option>Sylet</option>
<option>Rangpur</option>
</select>
<div class="invalid-feedback">Please select a valid state.</div>
</div>
<div class="col-md-3">
<label for="validationCustom05" class="form-label">Zip</label>
<input
type="text"
v-model="formInfo.zip"
@input="formInfo.zip = formInfo.zip.replace(/\D/g, '')"
:class="{
'is-invalid': zipValid === false,
'is-valid': zipValid === true
}"
class="form-control"
id="validationCustom05"
@blur="handleFocus('zip')"
required
/>
<div class="invalid-feedback">Please provide a valid zip.</div>
</div>
<div class="col-12">
<div class="form-check">
<input
class="form-check-input"
:class="{
'is-invalid': termsValid === false,
'is-valid': termsValid === true
}"
type="checkbox"
v-model="formInfo.terms"
@blur="handleFocus('terms')"
required
/>
<label class="form-check-label" for="invalidCheck">
Agree to terms and conditions
</label>
<div class="invalid-feedback" id="invalidCheck">
You must agree before submitting.
</div>
</div>
</div>
<div class="col-12">
<button class="btn btn-primary" type="submit">Submit form</button>
</div>
</form>
</div>
<script>
const { createApp, ref, watch, computed } = Vue;
createApp({
setup() {
const formInfo = ref({
firstName: "",
lastName: "",
username: "",
city: "",
state: "",
zip: "",
terms: false,
});
const touchedFields = ref({
firstName: false,
lastName: false,
username: false,
city: false,
state: false,
zip: false,
terms: false,
});
function handleFocus(fieldName) {
touchedFields.value[fieldName] = true;
}
const firstNameValid = computed(() => {
if (!touchedFields.value.firstName) return null;
return formInfo.value.firstName.trim().length > 0;
});
const lastNameValid = computed(() => {
if (!touchedFields.value.lastName) return null;
return formInfo.value.lastName.trim().length > 0;
});
const userNameValid = computed(() => {
if (!touchedFields.value.username) return null;
return formInfo.value.username.trim().length > 0;
});
const cityValid = computed(() => {
if (!touchedFields.value.city) return null;
return formInfo.value.city.trim().length > 0;
});
const stateValid = computed(() => {
if (!touchedFields.value.state) return null;
return formInfo.value.state.trim().length > 0;
});
const zipValid = computed(() => {
if (!touchedFields.value.zip) return null;
return formInfo.value.zip.trim().length > 0;
});
const termsValid = computed(() => {
if (!touchedFields.value.terms) return null;
return formInfo.value.terms;
});
function validateForm(event) {
event.preventDefault();
// Set all fields as touched
Object.keys(touchedFields.value).forEach((field) => {
touchedFields.value[field] = true;
});
if (
firstNameValid.value &&
lastNameValid.value &&
userNameValid.value &&
cityValid.value &&
stateValid.value &&
zipValid.value &&
termsValid.value
) {
alert(
"Form submitted successfully! Thank you, " +
formInfo.value.firstName +
" " +
formInfo.value.lastName +
", your username is " +
formInfo.value.username +
". You are from " +
formInfo.value.city +
", " +
formInfo.value.state +
", " +
formInfo.value.zip +
". You have agreed to the terms."
);
console.log({
firstName: formInfo.value.firstName,
lastName: formInfo.value.lastName,
username: formInfo.value.username,
city: formInfo.value.city,
state: formInfo.value.state,
zip: formInfo.value.zip,
terms: formInfo.value.terms,
});
} else {
alert("Please fill out all required fields correctly.");
console.log("Form validation failed");
}
}
return {
formInfo,
firstNameValid,
lastNameValid,
userNameValid,
cityValid,
stateValid,
zipValid,
termsValid,
validateForm,
handleFocus,
};
},
}).mount("#app");
</script>
<script
src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
crossorigin="anonymous"
></script>
</body>
</html>