-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
70 lines (62 loc) · 2.32 KB
/
script.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
65
66
67
68
69
70
const formTitle = document.getElementById("form-title");
const confirmPasswordContainer = document.getElementById("confirm-password-container");
const submitButton = document.getElementById("submit");
const toggleLink = document.getElementById("toggle-link");
const usernameInput = document.getElementById("username");
const passwordInput = document.getElementById("password");
const confirmPasswordInput = document.getElementById("confirm-password");
function toggleAuth() {
const isLoginForm = formTitle.textContent === "Login";
formTitle.textContent = isLoginForm ? "Sign Up" : "Login";
submitButton.textContent = isLoginForm ? "Sign Up" : "Login";
toggleLink.textContent = isLoginForm ? "Login" : "Sign Up";
confirmPasswordContainer.style.display = isLoginForm ? "block" : "none";
}
const user = [];
function login (username, password) {
if(username === "" || password === "") {
alert("Please enter the username and/or password.")
} else {
const foundUser = user.find(user => user.username === username && user.password === password);
if (foundUser) {
alert("Login successful!");
} else {
alert("User not found! Please sign up.");
}
}
}
function isStrongPassword(password) {
const upperCaseRegex = /[A-Z]/;
const lowerCaseRegex = /[a-z]/;
const numberRegex = /[0-9]/;
const specialCharRegex = /[*&^%$#@!]/;
return (
password.length >= 8 &&
upperCaseRegex.test(password) &&
lowerCaseRegex.test(password) &&
numberRegex.test(password) &&
specialCharRegex.test(password)
);
}
function signUp(username, password, confirmpassword) {
if(!isStrongPassword(password)) {
alert("Password must be at least 8 characters long and contain at least one uppercase letter, one lowercase letter, one number, and one special character.");
} else if(password !== confirmpassword) {
alert("Password don't match.");
} else {
user.push({username: username, password: password});
alert("Sign up successful! You can now login.");
toggleAuth();
}
}
function handleSubmit() {
const isLoginForm = formTitle.textContent === "Login";
const username = usernameInput.value;
const password = passwordInput.value;
const confirmPassword = confirmPasswordInput.value;
if(isLoginForm) {
login(username, password);
} else {
signUp(username, password, confirmPassword)
}
}