-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathscript.js
More file actions
119 lines (94 loc) · 3.12 KB
/
Copy pathscript.js
File metadata and controls
119 lines (94 loc) · 3.12 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
"use strict";
let reviews = document.getElementById("reviews");
let contacts = document.getElementById("contacts");
let product = document.getElementById("products");
let login = document.getElementById("login");
let loginPage = document.querySelector(".loginPage");
let logout = document.getElementById("logout"); // Add this button in your HTML
// --- Validation Helpers ---
function isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
function isValidPassword(password) {
const passRegex = /^(?=.*[A-Z])(?=.*\d).{6,}$/;
return passRegex.test(password);
}
// --- Show Login Page ---
login.addEventListener("click", function () {
loginPage.style.display = "block";
});
// --- LOGIN FUNCTIONALITY ---
let loged = document.getElementById("loged");
loged.addEventListener("click", function () {
let email = document.getElementById("email");
let pass = document.getElementById("pass");
if (!email.value || !pass.value) {
alert("Please enter both email and password.");
return;
}
if (!isValidEmail(email.value)) {
alert("Please enter a valid email address.");
return;
}
if (!isValidPassword(pass.value)) {
alert("Password must be at least 6 characters long, contain one uppercase letter and one number.");
return;
}
// Store login data (for demo; do NOT store real passwords this way)
localStorage.setItem("userEmail", email.value);
alert("You are logged in!");
loginPage.style.display = "none";
updateLoginState();
});
// --- CONTACT FORM ---
let submit = document.getElementById("submit");
submit.addEventListener("click", function () {
let nameField = document.getElementById("name");
let passwordField = document.getElementById("pass");
if (!nameField.value || !passwordField.value) {
alert("Please enter both name and password.");
return;
}
if (nameField.value.length < 3) {
alert("Name must be at least 3 characters long.");
return;
}
if (!isValidPassword(passwordField.value)) {
alert("Password must be at least 6 characters long, contain one uppercase letter and one number.");
return;
}
alert("Thanks for connecting!");
});
// --- LOGOUT FUNCTIONALITY ---
logout.addEventListener("click", function () {
localStorage.removeItem("userEmail");
alert("You have been logged out!");
updateLoginState();
});
// --- AUTO LOGIN STATE CHECK ---
function updateLoginState() {
const userEmail = localStorage.getItem("userEmail");
if (userEmail) {
login.style.display = "none";
logout.style.display = "inline-block";
console.log(`Logged in as: ${userEmail}`);
} else {
login.style.display = "inline-block";
logout.style.display = "none";
}
}
// --- Initialize State on Page Load ---
window.addEventListener("DOMContentLoaded", updateLoginState);
// --- NAVIGATION ---
reviews.addEventListener("click", function () {
window.location.href = "reviews.html";
});
contacts.addEventListener("click", function () {
window.location.href = "contact.html";
});
product.addEventListener("click", function () {
window.location.href = "products.html";
});
// --- IGNORE ---
// End of script.js