-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
72 lines (59 loc) · 2.02 KB
/
Copy pathscript.js
File metadata and controls
72 lines (59 loc) · 2.02 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
const username = document.querySelector("#username");
const password = document.querySelector("#password");
const passwordConfirmation = document.querySelector("#password-confirmation");
const terms = document.querySelector("#terms");
const form = document.querySelector("#form-validation");
const errorsContainer = document.querySelector(".errors");
const errorsList = document.querySelector(".errors-list");
const showPassword= document.querySelector("#password-show");
const showPasswordConfirmation = document.querySelector("#password-confirmation-show");
//set initial value to hide passwords
showPassword.checked=false;
showPasswordConfirmation.checked=false;
form.addEventListener("submit", e => {
// array for error display
const errorMessages = [];
clearErrors();
if (username.value.length < 6) {
errorMessages.push("Username must be at least 6 characters.");
}
if (password.value.length < 10) {
errorMessages.push("Password must be at least 6 characters.");
}
if (password.value !== passwordConfirmation.value){
errorMessages.push("Passwords must match")
}
if (!terms.checked){
errorMessages.push("You must accept the terms");
}
if (errorMessages.length > 0){
e.preventDefault();
showErrors(errorMessages);
}
});
showPassword.addEventListener("click", e => {
if (showPassword.checked === true){
password.type = "text";
} else {
password.type = "password";
}
});
showPasswordConfirmation.addEventListener("click", e => {
if (showPasswordConfirmation.checked === true){
passwordConfirmation.type = "text";
} else {
passwordConfirmation.type = "password";
}
});
function clearErrors() {
errorsList.innerHTML= "";
errorsContainer.classList.remove("show");
};
function showErrors(errorMessages){
errorMessages.forEach(e => {
const li = document.createElement("li");
li.innerHTML = e;
errorsList.appendChild(li);
});
errorsContainer.classList.add("show");
};