-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
82 lines (65 loc) · 2.26 KB
/
Copy pathscript.js
File metadata and controls
82 lines (65 loc) · 2.26 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
const password = document.getElementById("password");
const confirmPassword = document.getElementById("confirmPassword");
const message = document.getElementById("message");
const form = document.getElementById("registrationForm");
const mdp = document.getElementById("mdp");
const submitButton = document.getElementById("btnValider");
function verifyPasswords() {
if (confirmPassword.value === "") {
message.textContent = "";
message.className = "";
return false;
}
const matches = password.value === confirmPassword.value;
message.textContent = matches
? "Les mots de passe correspondent."
: "Les mots de passe ne correspondent pas.";
message.className = matches ? "success" : "error";
return matches;
}
function longueurMdp() {
const estValide = password.value.length >= 8;
if (password.value === "") {
mdp.textContent = "";
return false;
}
mdp.textContent = estValide
? "Mot de passe valide."
: "Le mot de passe doit contenir au moins 8 caractères.";
return estValide;
}
function updateSubmitButton() {
const motsDePasseValides = longueurMdp() && verifyPasswords();
submitButton.disabled = !form.checkValidity() || !motsDePasseValides;
}
password.addEventListener("input", () => {
updateSubmitButton();
});
confirmPassword.addEventListener("input", () => {
updateSubmitButton();
});
form.addEventListener("input", updateSubmitButton);
form.addEventListener("change", updateSubmitButton);
document.querySelectorAll(".masque").forEach((checkbox) => {
checkbox.addEventListener("change", () => {
const input = document.getElementById(checkbox.dataset.target);
if (input) {
input.type = checkbox.checked ? "text" : "password";
}
});
});
form.addEventListener("submit", (event) => {
const motDePasseValide = longueurMdp();
const confirmationValide = verifyPasswords();
if (!motDePasseValide || !confirmationValide || !form.checkValidity()) {
event.preventDefault();
if (!motDePasseValide) {
password.focus();
} else if (!confirmationValide) {
confirmPassword.focus();
} else {
form.reportValidity();
}
}
});
updateSubmitButton();