-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
148 lines (123 loc) · 5.48 KB
/
Copy pathscript.js
File metadata and controls
148 lines (123 loc) · 5.48 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
document.addEventListener('DOMContentLoaded', () => {
// --- 1. Modal Control (DOM Manipulation) ---
const modal = document.getElementById('auth-modal');
const loginBtn = document.getElementById('login-btn');
const closeBtn = document.querySelector('.close-btn');
const showLoginBtn = document.getElementById('show-login');
const showRegisterBtn = document.getElementById('show-register');
const loginForm = document.getElementById('login-form');
const registerForm = document.getElementById('register-form');
// Function to open the modal
loginBtn.onclick = function() {
modal.style.display = 'block';
}
// Function to close the modal using the X button
closeBtn.onclick = function() {
modal.style.display = 'none';
}
// Function to close the modal when clicking outside of it
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = 'none';
}
}
// Tab switching logic
function switchTab(showForm, hideForm, showButton, hideButton) {
showForm.classList.add('active-form');
hideForm.classList.remove('active-form');
showButton.classList.add('active');
hideButton.classList.remove('active');
// Clear previous messages
document.getElementById('login-message').textContent = '';
document.getElementById('register-message').textContent = '';
}
showLoginBtn.onclick = () => switchTab(loginForm, registerForm, showLoginBtn, showRegisterBtn);
showRegisterBtn.onclick = () => switchTab(registerForm, loginForm, showRegisterBtn, showLoginBtn);
// --- 2. Input Field Validation ---
// Generic Validation Function
const validateEmail = (email) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
const validatePassword = (password) => password.length >= 8;
const displayMessage = (id, message) => document.getElementById(id).textContent = message;
// --- Login Form Validation ---
loginForm.addEventListener('submit', function(e) {
e.preventDefault();
const email = document.getElementById('login-email').value;
const password = document.getElementById('login-password').value;
let isValid = true;
let message = '';
if (!validateEmail(email)) {
message += 'Invalid email format. ';
isValid = false;
}
if (password.length === 0) {
message += 'Password is required.';
isValid = false;
}
displayMessage('login-message', message);
if (isValid) {
// In a real application, you would send data to a server here.
displayMessage('login-message', 'Login successful! (Simulated)');
// For prototype: Close modal after a short delay
setTimeout(() => modal.style.display = 'none', 1500);
}
});
// --- Registration Form Validation ---
registerForm.addEventListener('submit', function(e) {
e.preventDefault();
const username = document.getElementById('reg-username').value;
const email = document.getElementById('reg-email').value;
const password = document.getElementById('reg-password').value;
const confirmPassword = document.getElementById('reg-confirm-password').value;
let isValid = true;
let message = '';
if (username.length < 3) {
message += 'Username must be at least 3 characters. ';
isValid = false;
}
if (!validateEmail(email)) {
message += 'Invalid email format. ';
isValid = false;
}
if (!validatePassword(password)) {
message += 'Password must be at least 8 characters. ';
isValid = false;
}
if (password !== confirmPassword) {
message += 'Passwords do not match.';
isValid = false;
}
displayMessage('register-message', message);
if (isValid) {
// In a real application, this is where a new user would be created.
displayMessage('register-message', 'Registration successful! Please login.');
// Switch to login tab automatically
setTimeout(() => {
switchTab(loginForm, registerForm, showLoginBtn, showRegisterBtn);
// Clear the form fields for security/convenience
registerForm.reset();
}, 1000);
}
});
// --- 3. Dynamic Wishlist/Cart Interaction (Minimal DOM Demo) ---
document.querySelectorAll('.wishlist-btn').forEach(button => {
button.addEventListener('click', function() {
const icon = this.querySelector('i');
if (icon.classList.contains('far')) {
icon.classList.remove('far');
icon.classList.add('fas');
icon.style.color = '#F05454';
alert('Product added to Wishlist!');
} else {
icon.classList.remove('fas');
icon.classList.add('far');
icon.style.color = '';
alert('Product removed from Wishlist!');
}
});
});
document.querySelectorAll('.add-to-cart-btn').forEach(button => {
button.addEventListener('click', function() {
alert('Product added to Cart!');
});
});
});