forked from TouficS/CST8914-lab7
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion-2-3-webform.html
More file actions
359 lines (318 loc) · 12.7 KB
/
Question-2-3-webform.html
File metadata and controls
359 lines (318 loc) · 12.7 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Feedback form</title>
<meta charset="utf-8" />
<style>
.control,
fieldset {
margin: 6px 0;
}
label {
display: inline-block;
width: 120px;
vertical-align: top;
}
input + label {
width: auto;
}
.error,
.required {
color: red;
}
fieldset ul {
margin: 0;
}
/* Visually hidden utility for screen reader only instructions */
.sr-only {
position: absolute;
height: 1px;
width: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
/* Clear, visible focus for keyboard users */
:focus-visible {
outline: 3px solid #ffbf47;
outline-offset: 2px;
}
/* Error summary styling */
fieldset.errors {
border: 2px solid #c00;
padding: 8px 12px;
margin-bottom: 12px;
}
fieldset.errors legend {
font-weight: bold;
color: #c00;
}
fieldset.errors a {
text-decoration: underline;
}
/* Make inline help text slightly muted for sighted users */
.help {
display: block;
font-size: 0.95rem;
color: #444;
margin-left: 120px;
}
/* Success message (polite live region) */
#status {
display: none; /* hidden until success */
background-color: #e6ffed;
color: #006b3a;
border: 1px solid #8fd19e;
padding: 8px 12px;
font-weight: 600;
}
#status.visible {
display: block; /* shown when success */
}
</style>
</head>
<body>
<h1>Feedback form</h1>
<!-- Q2-3. Identifying required fields in text -->
<p>
<strong
>All fields marked with
<span class="required" aria-hidden="true">*</span> are required.</strong
>
<span class="sr-only"
>Required fields are indicated by the word "required".</span
>
</p>
<div id="status" role="status" aria-live="polite"></div>
<!-- Added method/action for clarity; novalidate so our custom JS demo runs consistently -->
<form method="get" action="" novalidate>
<div class="control">
<!-- Q2-1. Proper label association: add for/id.
Q2-2. Indicate required in text & programmatically.
Q2-4. Add required + aria-describedby for inline instructions. -->
<label for="name"
>Full name <span class="required" aria-hidden="true">*</span>
<span class="sr-only">(required)</span></label
><input
id="name"
name="name"
type="text"
required
autocomplete="name"
aria-describedby="name_help"
/>
<!-- Inline instruction visible and announced -->
<span id="name_help" class="help">Enter your first and last name.</span>
</div>
<div class="control">
<!-- Q2-1. Add for/id
Q2-4. Connect help via aria-describedby -->
<label for="biography"
>Biography <span class="required" aria-hidden="true">*</span>
<span class="sr-only">(required)</span></label
><textarea
id="biography"
name="biography"
aria-describedby="bio-help"
></textarea>
<span id="bio_help" class="help"
>Include examples on your skills in JavaScript.</span
>
</div>
<!-- Q2-2. & Q2-3. Group radios in a field/legend and indicate required in text -->
<!-- Q2-1. Associate each radio with its label via for/id -->
<fieldset class="control" aria-describedby="gender_help">
<legend>
Gender <span class="required" aria-hidden="true">*</span>
<span class="sr-only">(required)</span>
</legend>
<!-- Q2-4. Inline instruction for SRs and visual -->
<span id="gender_help" class="help">Choose one option.</span>
<input id="gender_male" name="gender" type="radio" value="male" />
<label for="gender_male">Male</label>
<input id="gender_female" name="gender" type="radio" value="female" />
<label for="gender_female">Female</label>
</fieldset>
<div class="control">
<!-- Q2-1. Label connected to checkbox with for/id -->
<input
id="accept_agbs"
name="accept_agbs"
type="checkbox"
value="1"
aria-describedby="terms-help"
/>
<label for="accept_agbs">I accept the terms and conditions</label>
<span id="terms_help" class="help"
>You must accept before registering</span
>
</div>
<div class="control">
<!-- Keep the original validate=1 mechanism so the URL-param demo works -->
<input name="validate" type="hidden" value="1" />
<input id="submitBtn" type="submit" value="Register" />
</div>
</form>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"
></script>
<script>
"use strict";
(function () {
var validateInput;
// Helper to read URL param for this demo validation
// Define a helper $.urlParam(name) that reads query-string parameters from the page URL
$.urlParam = function (name) {
var results = new RegExp("[?&]" + name + "=([^&#]*)").exec(
window.location.href
);
if (results === null) return null;
// Replace '+' with ' ' when retrieve name in text box
return decodeURIComponent(results[1].replace(/\+/g, " ")) || null;
};
// };
// Q3-2 & Q3-3: build an accessible error summary, link each message to its control,
// Ensure focus moves to the summary. Also ensure aria-describedby exist.
// Define validateInput(input, message) which:
// - if the parameter is missing -> creates/updates an error summary and links the error to the field;
// - if the parameter is present -> fills the field from the URL (so the form "remembers" entries)
validateInput = function validateInput(input, message) {
var $elementToDescribe,
$error,
$errorContainer,
$fieldset,
$input,
$referencedElement,
value;
$input = $('[name="' + input + '"]'); // jQuery select by name
if ((value = $.urlParam(input)) === null) {
// MISSING value -> error mode
// Create (once) a tabbable, live error summary at the top
// Ensure an error summary exists at the top
if ($("fieldset.errors").length === 0) {
$("form").prepend(
// Q3-2: role="alert" + aria-live assertively announces errors;
// Q3-3: tabindex="-1" so we can programmatically focus the summary.
'<fieldset class="errors" role="alert" aria-live="assertive" tabindex="-1">' +
'<legend id="error_legend">Errors</legend><ul></ul></fieldset>'
);
}
// Find the <ul> inside the error summary fieldset
$errorContainer = $("fieldset.errors ul");
// Pick the element to describe + the element to focus
if ($input.is(":radio")) {
// For radio groups, describe the whole fieldset
$fieldset = $input.closest("fieldset");
$elementToDescribe = $fieldset.length ? $fieldset : $input;
$referencedElement = $input.filter(":first"); // first radio will get focus
} else {
$elementToDescribe = $input; // describe this control
$referencedElement = $input; // and focus it from the summary link
}
// Ensure the described-by element exists; create it if needed
var descId = input + "_description";
if ($("#" + descId).length === 0) {
// Create a hidden description span and connect it
var hiddenSpan = $(
'<span id="' + descId + '" class="sr-only"></span>'
);
// Insert it after the control (or for radios, after the first radio)
if ($input.is(":radio")) {
$referencedElement.after(hiddenSpan);
} else {
$elementToDescribe.after(hiddenSpan);
}
}
$elementToDescribe.attr("aria-describedby", function (i, val) {
// Preserve any existing help ids (like *_help) by appending the error id
return (val ? val + " " : "") + descId;
});
// Q3-2: FIX the “hint” line - make the summary item an anchor pointing to the control
// If referenced element has no id, ensure it has one
if (!$referencedElement.attr("id")) {
$referencedElement.attr("id", input + "_field");
}
// R.G. hint: fix next line ;)
$error = $(
'<a href="#' +
$referencedElement.attr("id") +
'">' +
message +
"</a>"
);
// When clicking an error link, move focus to the referenced control
$error.on("click", function (e) {
// If we rely on the link's href pointing to the input's ID, it doesn't trigger forms mode in screen readers
// Moving focus triggers forms/browse modes correctly in SRs
$referencedElement.focus();
e.preventDefault();
});
//$elementToDescribe.attr("aria-describedby", input + "_description");
$errorContainer.append("<li>").find("li:last").append($error);
//if ($(":not(form):focus").length === 0) {
// See https://stackoverflow.com/questions/46134247/jquery-setting-focus-doesnt-work-in-ie11/
//}
} else {
// PRESENT value -> fill the field
// handle checkbox
if ($input.is(":checkbox")) {
$input.attr("checked", true);
}
// handle radio buttons
if ($input.is(":radio")) {
return $input
.filter('[value="' + value + '"]')
.attr("checked", true);
} else {
// handle all other input types
return $input.val(value);
}
}
};
// When the page loads after pressing Register (method=get), run the validation
$(document).ready(function () {
if ($.urlParam("validate")) {
// Clear previous status message
$("#status").removeClass("visible").empty();
// On $(document).ready(...):
// - check if ?validate=1 is in the URL,
// - run validateInput(...) for each field,
//Q2-1..Q2-4 messages that also appear in the error summary
validateInput("name", "Please enter your name!");
validateInput(
"biography",
"Please tell us something about your history!"
);
validateInput("gender", "Please tell us your gender!");
validateInput(
"accept_agbs",
"You must accept our terms and conditions!"
);
var hasErrors = $("fieldset.errors li").length > 0;
if (hasErrors) {
// Q3-3: Move focus to the error summary for keyboard and SR users
// if any errors exist -> move focus to summary
var $summary = $("fieldset.errors");
// Add error count to legend for clarity
var count = $("fieldset.errors li").length;
$("#error_legend").text("Errors (" + count + ")");
$summary.focus();
// Ensure success message stays hidden if there are errors
$("#status").removeClass("visible").empty();
} else {
// Q3-1: Only announce success when there are no errors
// Show both the on-screen message and pop-up alert
// Show visible success message only when all inputs are valid
$("#status").addClass("visible").text("All inputs are valid.");
alert("All inputs are valid.");
}
}
});
})();
</script>
</body>
</html>