-
Notifications
You must be signed in to change notification settings - Fork 0
/
form.js
62 lines (57 loc) · 1.9 KB
/
form.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
const form = document.querySelector('form');
const email = document.querySelector('input[type="email"]');
const user = document.querySelector('input[type="text"]');
const spanMail = document.querySelector('#mail');
const spanUser = document.querySelector('#user');
/* A function for the email's errors */
function showErrorMail() {
/* If the email input is empty */
if (email.validity.valueMissing) {
spanMail.textContent = '* Please enter a value.';
} else if (email.value !== email.value.toLowerCase()) {
/* If the input is not in lower case */
spanMail.textContent = '* Only enter characters in lower case.';
} else if (email.validity.typeMismatch) {
/* If the input is not an email type */
spanMail.textContent = '* Please enter a valid email.';
}
}
/* A function for the User's name errors */
function showErrorUser() {
if (user.validity.valueMissing) {
spanUser.textContent = '* Please enter a value.';
} else if (user.validity.typeMismatch) {
spanUser.textContent = '* Please enter a valid name.';
}
}
/* A input listener to the email input */
email.addEventListener('input', () => {
if (email.validity.valid) {
/* If it's valid the text of the error content dissapears */
spanMail.textContent = '';
spanMail.className = 'error';
} else {
/* If not, then displays the errors */
showErrorMail();
}
});
/* A input listener to the user input */
user.addEventListener('input', () => {
if (user.validity.valid) {
spanUser.textContent = '';
spanUser.className = 'error';
} else {
showErrorUser();
}
});
/* A submit listener to the form element */
form.addEventListener('submit', (event) => {
if (!user.validity.valid) {
showErrorUser();
event.preventDefault();
} else if (!email.validity.valid) {
/* If it's not valid, displays the error and avoids the form to be submmitted. */
showErrorMail();
event.preventDefault();
}
});