-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
189 lines (148 loc) · 5.7 KB
/
script.js
File metadata and controls
189 lines (148 loc) · 5.7 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// const toggleFormButton = document.getElementById('toggleFormButton');
// const formContainer = document.getElementById('formContainer');
// const formTitle = document.getElementById('formTitle');
// const form = document.getElementById('form');
// const toggleFormType = document.getElementById('toggleFormType');
// let isSignup = true;
// // Show/Hide Form
// toggleFormButton.addEventListener('click', () => {
// formContainer.style.display = formContainer.style.display === 'none' ? 'block' : 'none';
// updateForm();
// });
// // Switch between Signup/Login
// toggleFormType.addEventListener('click', () => {
// isSignup = !isSignup;
// updateForm();
// });
// function updateForm() {
// form.innerHTML = '';
// formTitle.textContent = isSignup ? 'Sign Up' : 'Login';
// if (isSignup) {
// form.innerHTML = `
// <input type="email" id="email" placeholder="Email" required />
// <input type="password" id="password" placeholder="Password" required />
// <input type="text" id="mobNo" placeholder="Mobile Number" required />
// <button type="submit">Sign Up</button>`;
// } else {
// form.innerHTML = `
// <input type="email" id="loginEmail" placeholder="Email" required />
// <input type="password" id="loginPassword" placeholder="Password" required />
// <button type="submit">Login</button>`;
// }
// attachSubmitHandler();
// }
// function attachSubmitHandler() {
// form.addEventListener('submit', (event) => {
// event.preventDefault();
// if (isSignup) {
// const email = document.getElementById('email').value;
// const password = document.getElementById('password').value;
// const mobNo = document.getElementById('mobNo').value;
// fetch('http://localhost:5000/users', {
// method: 'POST',
// headers: { 'Content-Type': 'application/json' },
// body: JSON.stringify({ email, password, mobNo }),
// })
// .then((res) => res.json())
// .then((data) => alert(data.message))
// .catch(console.error);
// } else {
// const email = document.getElementById('loginEmail').value;
// const password = document.getElementById('loginPassword').value;
// fetch('http://localhost:5000/users/login', {
// method: 'POST',
// headers: { 'Content-Type': 'application/json' },
// body: JSON.stringify({ email, password }),
// })
// .then((res) => res.json())
// .then((data) => alert(data.message))
// .catch(console.error);
// }
// });
// }
document.getElementById('goToSignup').addEventListener('click', function (event) {
event.preventDefault();
document.getElementById('signupForm').style.display = 'block'; // Show Sign Up
document.getElementById('loginForm').style.display = 'none'; // Hide Login
const errorMessage = document.getElementById('errorMessage');
});
// Show Login Form and hide Sign Up Form
document.getElementById('goToLogin').addEventListener('click', function (event) {
event.preventDefault();
document.getElementById('signupForm').style.display = 'none'; // Hide Sign Up
document.getElementById('loginForm').style.display = 'block'; // Show Login
});
// Sign up form submission handler
document.getElementById('signup').addEventListener('submit', function (event) {
event.preventDefault();
const email = document.getElementById('email').value.trim();
const password = document.getElementById('password').value.trim();
const mobNo = document.getElementById('mobNo').value.trim();
let isValid = true;
if (!email.includes('@')) {
displayError('email', 'Please enter a valid email.');
isValid = false;
}
if (password.length < 6) {
displayError('password', 'Password must be at least 6 characters long.');
isValid = false;
}
if (mobNo.length !== 10 || isNaN(mobNo)) {
displayError('mobNo', 'Mobile number must be 10 digits long.');
isValid = false;
}
if (isValid) {
// Make POST request to save sign-up details
fetch('http://localhost:5000/users', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password, mobNo })
})
.then(response => response.json())
.then(data => {
alert(data.message);
document.getElementById('signup').reset();
})
.catch(error => console.error('Error:', error));
}
});
// Login form submission handler
document.getElementById('login').addEventListener('submit', function (event) {
event.preventDefault();
const email = document.getElementById('loginEmail').value.trim();
const password = document.getElementById('loginPassword').value.trim();
let isValid = true;
if (!email.includes('@')) {
displayError('loginEmail', 'Please enter a valid email.');
isValid = false;
}
if (password.length < 6) {
displayError('loginPassword', 'Password must be at least 6 characters long.');
isValid = false;
}
if (isValid) {
// Make POST request to login
fetch('http://localhost:5000/users/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ email, password })
})
.then(response => response.json())
.then(data => {
alert(data.message);
})
.catch(error => console.error('Error:', error));
}
});
function displayError(fieldId, message) {
const field = document.getElementById(fieldId);
const errorElement = document.createElement('span');
errorElement.style.color = 'red';
errorElement.textContent = message;
const existingError = field.parentNode.querySelector('.error');
if (existingError) {
existingError.remove();
}
field.parentNode.appendChild(errorElement);
errorElement.classList.add('error');
}