-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (54 loc) · 1.94 KB
/
index.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
const wrapper = document.querySelector('.wrapper');
const btnPopup = document.querySelector('.btnlogin-popup');
const iconClose = document.querySelector('.icon-close');
const form = document.querySelector('form');
const emailInput = document.querySelector('input[type="email"]');
const passwordInput = document.querySelector('input[type="password"]');
const roleSelect = document.querySelector('select');
const emailError = document.createElement('div');
const passwordError = document.createElement('div');
const roleError = document.createElement('div');
emailError.style.color = 'red';
passwordError.style.color = 'red';
roleError.style.color = 'red';
// Show the popup when the "Login" button is clicked
btnPopup.addEventListener('click', () => {
wrapper.classList.add('active-popup');
});
// Hide the popup when the close icon is clicked
iconClose.addEventListener('click', () => {
wrapper.classList.remove('active-popup');
});
// Form validation
form.addEventListener('submit', (e) => {
let valid = true;
// Reset error messages
emailError.textContent = '';
passwordError.textContent = '';
roleError.textContent = '';
// Email validation
if (!validateEmail(emailInput.value)) {
emailError.textContent = 'Please enter a valid email address.';
emailInput.parentNode.appendChild(emailError);
valid = false;
}
// Password validation
if (passwordInput.value.length < 6) {
passwordError.textContent = 'Password must be at least 6 characters long.';
passwordInput.parentNode.appendChild(passwordError);
valid = false;
}
// Role validation
if (roleSelect.value === '') {
roleError.textContent = 'Please select a role.';
roleSelect.parentNode.appendChild(roleError);
valid = false;
}
if (!valid) {
e.preventDefault();
}
});
function validateEmail(email) {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}