-
Notifications
You must be signed in to change notification settings - Fork 0
/
subscribe.js
38 lines (26 loc) · 1.14 KB
/
subscribe.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
const subscribeForm = document.getElementById('subscribe-form');
const subscribeMessage = document.getElementById('subscribe-message');
subscribeForm.addEventListener('submit', (event) => {
event.preventDefault();
const email = document.getElementById('email').value;
if (!validateEmail(email)) {
subscribeMessage.textContent = "Please enter a valid email address.";
return;
}
const existingEmails = getEmailsFromLocalStorage();
existingEmails.push(email);
storeEmailsInLocalStorage(existingEmails);
subscribeMessage.textContent = "You have subscribed successfully!";
document.getElementById('email').value = '';
});
function validateEmail(email) {
const re = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(String(email).toLowerCase());
}
function getEmailsFromLocalStorage() {
const storedEmails = localStorage.getItem('emails');
return storedEmails ? JSON.parse(storedEmails) : [];
}
function storeEmailsInLocalStorage(emails) {
localStorage.setItem('emails', JSON.stringify(emails));
}