-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsignup.js
More file actions
25 lines (19 loc) · 847 Bytes
/
signup.js
File metadata and controls
25 lines (19 loc) · 847 Bytes
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
document.getElementById("signupForm").addEventListener("submit", function (e) {
e.preventDefault();
const username = document.getElementById("newUsername").value.trim();
const password = document.getElementById("newPassword").value.trim();
const errorMsg = document.getElementById("signup-error-msg");
// Get existing users or empty array
const users = JSON.parse(localStorage.getItem("users")) || [];
// Check if username already exists
const userExists = users.some(user => user.username === username);
if (userExists) {
errorMsg.textContent = "Username already exists.";
return;
}
// Add new user
users.push({ username, password });
localStorage.setItem("users", JSON.stringify(users));
alert("Account created! You can now log in.");
window.location.href = "index.html";
});