-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
49 lines (41 loc) · 1.36 KB
/
script.js
File metadata and controls
49 lines (41 loc) · 1.36 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
const form = document.getElementById("form");
const username = document.getElementById("username");
const password = document.getElementById("password");
const passwordConfirm = document.getElementById("password-confirmation");
const terms = document.getElementById("terms");
const errorContainer = document.querySelector(".errors");
const errorList = document.querySelector(".errors-list");
form.addEventListener("submit", (e) => {
let errorMessages = [];
clearErrors();
if (username.value.length < 6) {
errorMessages.push("Username must be at least 6 characters long!");
}
if (password.value.length < 10) {
errorMessages.push("Password must be at least 10 characters long!");
}
if (password.value !== passwordConfirm.value) {
errorMessages.push("Passwords must match!");
}
if (!terms.checked) {
errorMessages.push("You must agree to the TOS!");
}
if (errorMessages.length > 0) {
e.preventDefault();
showErrors(errorMessages);
}
});
function clearErrors() {
while (errorList.children[0] != null) {
errorList.removeChild(errorList.children[0]);
}
errorContainer.classList.remove("show");
}
function showErrors(errorMessages) {
errorMessages.forEach((errorMessage) => {
const li = document.createElement("li");
li.innerText = errorMessage;
errorList.appendChild(li);
});
errorContainer.classList.add("show");
}