-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
82 lines (64 loc) · 2.34 KB
/
script.js
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
// Create global variables
const form = document.querySelector("form");
const username = document.querySelector("#name");
const email = document.querySelector("#email");
const bdate = document.querySelector("#bdate");
const language = document.querySelector("#language");
const pass = document.querySelector("#pass");
const confirmPass = document.querySelector("#confirm-pass");
//const fieldNames = ["#name", "#email", "#bdate", "#language", "#pass", "#confirm-pass"];
const field = [username, email, bdate, language, pass, confirmPass];
//const usernameError = document.querySelector("#name + span.error");
form.addEventListener("submit", (event) => {
let validatedFields = 0;
const fieldLength = field.length;
for (i = 0; i < fieldLength; i++) {
//Check validity of each field.
if (checkValidity(field[i]) === true) {
validatedFields += 1;
};
}
//Check if all fields are valid.
if (validatedFields != fieldLength) {
event.preventDefault();
}
});
function checkValidity(field) {
showError(field);
if (!field.validity.valid) {
field.classList.remove("valid");
field.classList.add("invalid");
return false;
} else {
field.classList.remove("invalid");
field.classList.add("valid");
return true;
}
};
// Show a message error.
function showError(field) {
//Transform the constant username into a string "username".
let fieldString = field.id;
//Creating a variable fieldError for each fieldString.
let fieldError = document.querySelector(`#${fieldString} + span.error`);
console.log(fieldError);
if (field.validity.valueMissing) {
fieldError.textContent = "You must fill this field.";
} else if (field.validity.tooShort) {
fieldError.textContent = `This value should be at least ${field.minLength} characters.`;
} else if (field.validity.typeMismatch) {
fieldError.textContent = `This is not an ${fieldString}.`
} else {
fieldError.textContent = "";
}
};
//REAL-TIME VALIDATION
// username.addEventListener("input", (event) => {
// // Each time the user types something, we check if the
// // form fields are valid.
// if (username.validity.valid) {
// usernameError.textContent = "";
// } else {
// showError();
// }
// });