-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform_validation.js
65 lines (61 loc) · 2.41 KB
/
form_validation.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
// Use for sending messages to user about forms
const titleInput = document.getElementById('title');
const titleError = document.getElementById('title-error')
const authorInput = document.getElementById('author');
const authorError = document.getElementById('author-error')
const pagesInput = document.getElementById('pages');
const pagesError = document.getElementById('pages-error')
titleInput.addEventListener("input", (event) => {
// check if the form fields are valid.
if (titleInput.validity.valid) {
// if the field is valid, we remove the error message.
titleError.textContent = ""; // Reset the content of the message
titleError.className = "error"; // Reset the visual state of the message
} else {
// If there is still an error, show the correct error
showError();
}
});
authorInput.addEventListener("input", (event) => {
// check if the form fields are valid.
if (authorInput.validity.valid) {
// if the field is valid, we remove the error message.
authorError.textContent = ""; // Reset the content of the message
authorError.className = "error"; // Reset the visual state of the message
} else {
// If there is still an error, show the correct error
showError();
}
});
pagesInput.addEventListener("input", (event) => {
// check if the form fields are valid.
if (pagesInput.validity.valid) {
// if the field is valid, we remove the error message.
pagesError.textContent = ""; // Reset the content of the message
pagesError.className = "error"; // Reset the visual state of the message
} else {
// If there is still an error, show the correct error
showError();
}
});
function showError() {
if (titleInput.validity.valueMissing) {
// If the field is empty
titleError.textContent = 'You need to enter a book title';
titleError.className = 'error active';
}
if(authorInput.validity.valueMissing){
// If the field is empty
authorError.textContent = 'You need to enter an author';
authorError.className = 'error active';
}
if(pagesInput.validity.valueMissing){
// If the field is empty
pagesError.textContent = 'You need to enter the amount of pages';
pagesError.className = 'error active';
} else if (pagesInput.validity.rangeOverflow){
// If the number > 9999
pagesError.textContent = 'Woah! Too many pages!';
pagesError.className = 'error active';
}
}