-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform_submit.js
57 lines (52 loc) · 1.8 KB
/
form_submit.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
const PUBLIC_KEY = "YK3uWlwoypZyGnoCw";
emailjs.init(PUBLIC_KEY);
const contactNameInput = document.getElementById("contact-name");
const contactEmailInput = document.getElementById("contact-email");
const contactMessageInput = document.getElementById("contact-message");
const contactSend = document.getElementById("contact-send");
const contactWarningMsgs = document.querySelectorAll(".input-empty-warning");
const regExp = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
const validateContactInfo = () => {
if (!contactNameInput.value.trim()) {
contactNameInput.nextElementSibling.style.setProperty("opacity", "1");
return false;
}
if (!regExp.test(contactEmailInput.value)) {
contactEmailInput.nextElementSibling.style.setProperty("opacity", "1");
return false;
}
if (!contactMessageInput.value.trim()) {
contactMessageInput.nextElementSibling.style.setProperty("opacity", "1");
return false;
}
return true;
};
const clearWarning = () => {
contactWarningMsgs.forEach((ele) => {
ele.style.setProperty("opacity", "0");
});
};
const successfulSubmission = () => {
contactNameInput.value = "";
contactEmailInput.value = "";
contactMessageInput.value = "";
contactSend.textContent = "Thank You!";
contactSend.style.setProperty("background-color", "green");
contactSend.style.setProperty("color", "#fbf7ef");
setTimeout(() => {
contactSend.textContent = "Send";
contactSend.style.setProperty("background-color", "#fbf7ef");
contactSend.style.setProperty("color", "black");
}, 2000);
};
document
.getElementById("contact-form")
.addEventListener("submit", function (e) {
e.preventDefault();
clearWarning();
if (validateContactInfo()) {
emailjs.sendForm("portfolio_contact", "template_to_me", this).then(() => {
successfulSubmission();
});
}
});