-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
52 lines (45 loc) · 1.61 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
const inputEmail = document.querySelector(".input-email");
const btnSubmit = document.querySelector(".btn-submit");
const outputMsg = document.querySelector(".error-output");
const iconError = document.querySelector(".error-icon");
const darkModeToggle = document.getElementById("dark-mode-checkbox");
const root = document.documentElement;
const validateEmail = (email) => {
const emailRegex =
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.(com|org|net|mil|edu|biz|gov|info|name|museum|[a-zA-Z]{2})$/;
return emailRegex.test(email);
};
btnSubmit.addEventListener("click", function (e) {
e.preventDefault();
let message = "";
const userEmail = inputEmail.value;
if (userEmail === "") {
message = "Please input your email.";
outputDesign("error");
} else if (validateEmail(userEmail)) {
message = "Thank you for subscribing!";
outputDesign("success");
inputEmail.value = "";
} else {
message = "Please provide a valid email.";
outputDesign("error");
}
return (outputMsg.textContent = message);
});
const outputDesign = function (status) {
if (status === "error") {
console.log("error");
iconError.style.display = "block";
outputMsg.style.color = "var(--soft-red)";
inputEmail.style.borderWidth = "2px";
inputEmail.style.borderColor = "var(--soft-red)";
} else if (status === "success") {
inputEmail.style.borderColor = "var(--desaturated-red)";
iconError.style.display = "none";
outputMsg.style.color = "var(--success)";
inputEmail.style.borderWidth = "1px";
}
};
darkModeToggle.addEventListener("change", function () {
root.classList.toggle("dark-mode");
});