-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (89 loc) · 2.84 KB
/
index.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
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
const InputFirstName = document.getElementById("first-name-input");
const InputLastName = document.getElementById("last-name-input");
const InputEmail = document.getElementById("email-input");
const submitButton1 = document.getElementById("submit-button");
const addButton = document.getElementById("add-button");
const listContainer = document.getElementById("list-container");
let isFirstNameValid = false;
let isLastNameValid = false;
let isEmailValid = false;
const subName = document.getElementById("sub-name");
const subType = document.getElementById("sub-type");
const startDate = document.getElementById("start-date");
const endDate = document.getElementById("end-date");
function validFirstName() {
const firstNameError = document.getElementById("first-name-feedback");
if (!InputFirstName.value) {
firstNameError.innerHTML = "Must enter a valid first name";
isFirstNameValid = false;
} else {
firstNameError.innerHTML = "";
isFirstNameValid = true;
}
}
function validLastName() {
const lastNameError = document.getElementById("last-name-feedback");
if (!InputLastName.value) {
lastNameError.innerHTML = "Must Enter a valid Last name";
isLastNameValid = false;
} else {
lastNameError.innerHTML = "";
isLastNameValid = true;
}
}
function validEmail() {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const email = InputEmail.value;
const emailError = document.getElementById("email-feedback");
if (!email.match(emailRegex)) {
emailError.innerHTML = "Invalid email entered";
// alert("please enter a valid email");
isEmailValid = false;
} else {
emailError.innerHTML = "";
isEmailValid = true;
}
}
submitButton1.addEventListener("click", (event) => {
// e is the call back function that will be exce when the event occurs
event.preventDefault();
validFirstName();
validLastName();
validEmail();
if (!isFirstNameValid || !isLastNameValid || !isEmailValid) {
alert("Please enter all required information");
} else {
alert(`Thanks ${InputFirstName.value}`);
console.log("signed in");
}
});
//function to add subscription to subs inventory
let subList = [];
function addToList(name, type) {
let existing = subList.find(
(sub) => sub.name.toLowerCase() === name.toLowerCase()
);
if (existing) {
alert("You have already added that subscription");
return;
}
subList.push({ name, type });
updateSubs();
}
function updateSubs(){
listContainer.innerHTML = ''
subList.forEach((sub) => {
const list = document.createElement('li')
list.textContent = `${sub.name} and ${sub.type}`;
listContainer.appendChild(list)
})
}
addButton.addEventListener("click", () => {
const name = subName.value;
const type = subType.value;
if (name && type) {
addToList(name, type);
} else {
alert("please fill in both name and type of sub");
}
});