-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
92 lines (80 loc) · 2.88 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
83
84
85
86
87
88
89
90
91
92
// https://developer.mozilla.org/en-US/docs/Learn/Forms/Form_validation
const form = document.querySelector('form');
const input = document.getElementsByTagName('input');
const emailRegExp =
/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
window.addEventListener('load', () => {
input.className = 'valid';
});
form.addEventListener('submit', (event) => {
event.preventDefault();
checkFirstNameField();
checkLastNameField();
checkEmailField();
checkPasswordField();
});
const checkFirstNameField = () => {
const firstNameInput = document.getElementById('first-name-input');
const firstNameErrorMsg = document.getElementById('first-name-error-msg');
if (firstNameInput.value === '') {
firstNameErrorMsg.style.display = 'block';
firstNameInput.className = 'invalid';
firstNameInput.placeholder = '';
} else {
firstNameErrorMsg.style.display = 'none';
firstNameInput.className = 'valid';
firstNameInput.placeholder = 'First Name';
}
};
const checkLastNameField = () => {
const lastNameInput = document.getElementById('last-name-input');
const lastNameErrorMsg = document.getElementById('last-name-error-msg');
if (lastNameInput.value === '') {
lastNameErrorMsg.style.display = 'block';
lastNameInput.className = 'invalid';
lastNameInput.placeholder = '';
} else {
lastNameErrorMsg.style.display = 'none';
lastNameInput.className = 'valid';
lastNameInput.placeholder = 'Last Name';
}
};
const checkEmailField = () => {
const emailInput = document.getElementById('email-input');
const emptyEmailErrorMsg = document.getElementById('empty-email-error-msg');
const invalidEmailErrorMsg = document.getElementById(
'invalid-email-error-msg'
);
if (emailInput.value.length === 0) {
emptyEmailErrorMsg.style.display = 'block';
emailInput.className = 'invalid';
emailInput.placeholder = 'email@example/com';
invalidEmailErrorMsg.style.display = 'none';
} else if (
!emailInput.value.includes('@') ||
!emailInput.value.includes('.')
) {
invalidEmailErrorMsg.style.display = 'block';
emailInput.placeholder = 'email@example/com';
emptyEmailErrorMsg.style.display = 'none';
emailInput.className = 'invalid';
} else {
emptyEmailErrorMsg.style.display = 'none';
invalidEmailErrorMsg.style.display = 'none';
emailInput.className = 'valid';
emailInput.placeholder = 'Email Address';
}
};
const checkPasswordField = () => {
const passwordInput = document.getElementById('password-input');
const passwordErrorMsg = document.getElementById('password-error-msg');
if (passwordInput.value === '') {
passwordErrorMsg.style.display = 'block';
passwordInput.className = 'invalid';
passwordInput.placeholder = '';
} else {
passwordErrorMsg.style.display = 'none';
passwordInput.className = 'valid';
passwordInput.placeholder = 'Password';
}
};