Skip to content

Commit 3dfcbf4

Browse files
committed
TD-5308-Validation added to check for whitespaces while adding new brands and categories.
1 parent f4b766c commit 3dfcbf4

File tree

1 file changed

+38
-0
lines changed
  • DigitalLearningSolutions.Web/Scripts/frameworks

1 file changed

+38
-0
lines changed

DigitalLearningSolutions.Web/Scripts/frameworks/branding.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ function brandChanged() {
1010
if (style === 'block') {
1111
tb.value = '';
1212
tb.required = true;
13+
tb.setCustomValidity("");
1314
tb.focus();
1415
} else {
16+
tb.value = "";
17+
tb.setCustomValidity("");
1518
tb.required = false;
1619
}
1720
}
@@ -27,8 +30,11 @@ function categoryChanged() {
2730
if (style === 'block') {
2831
tb.value = '';
2932
tb.required = true;
33+
tb.setCustomValidity("");
3034
tb.focus();
3135
} else {
36+
tb.value = "";
37+
tb.setCustomValidity("");
3238
tb.required = false;
3339
}
3440
}
@@ -49,3 +55,35 @@ function topicChanged() {
4955
tb.required = false;
5056
}
5157
}
58+
const form = document.querySelector("form") as HTMLFormElement;
59+
const bfield = <HTMLInputElement>document.getElementById('brand-field');
60+
const cfield = <HTMLInputElement>document.getElementById('category-field');
61+
function wireClearOnInput(input: HTMLInputElement) {
62+
input.addEventListener("input", () => {
63+
input.setCustomValidity("");
64+
});
65+
}
66+
67+
wireClearOnInput(bfield);
68+
wireClearOnInput(cfield);
69+
70+
form.addEventListener("submit", (e: Event) => {
71+
const fields = [
72+
{ el: bfield, msg: "Please enter a valid brand." },
73+
{ el: cfield, msg: "Please enter a valid category." }
74+
];
75+
76+
for (const f of fields) {
77+
if (f.el.required) {
78+
if (!f.el.value.trim()) {
79+
e.preventDefault();
80+
f.el.setCustomValidity(f.msg);
81+
f.el.reportValidity();
82+
f.el.focus();
83+
return;
84+
} else {
85+
f.el.setCustomValidity("");
86+
}
87+
}
88+
}
89+
});

0 commit comments

Comments
 (0)