-
Notifications
You must be signed in to change notification settings - Fork 0
/
contacts.js
94 lines (83 loc) · 2.97 KB
/
contacts.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
const form = document.querySelector("form");
const fullName = document.getElementById("name");
const email = document.getElementById("email");
const phone = document.getElementById("phone");
const subject = document.getElementById("subject");
const mess = document.getElementById("message");
function sendEmail() {
const bodyMessage = `Full Name: ${fullName.value}<br> Email: ${email.value}<br> Phone Number: ${phone.value}<br> Message: ${mess.value}`;
Email.send({
SecureToken: "0a47409f-e636-4cf6-8b8a-be55d9aa2ad5", //SMTPjs
// Host: "smtp.elasticemail.com",
// Username: "[email protected]",
// Password: "0A1C25ED16C32866DBAD0578D432D201C020",
To: '[email protected]',
From: "[email protected]",
// From: email.value,
Subject: subject.value,
Body: bodyMessage
}).then(
message => {
if (message=="OK"){
Swal.fire({ //sweetalert2
title: "Success!",
text: "Message sent successfully",
icon: "success"
});
}
}
);
}
function checkInputs() {
const items = document.querySelectorAll(".item");
for (const item of items) {
if (item.value == "") {
item.classList.add("error");
item.parentElement.classList.add("error");
}
if(items[1].value!=""){
checkEmail();
}
items[1].addEventListener("keyup",()=>{
checkEmail();
});
item.addEventListener("keyup", () => {
if (item.value != "") {
item.classList.remove("error");
item.parentElement.classList.remove("error");
}
else {
item.classList.add("error");
item.parentElement.classList.add("error");
}
});
}
}
function checkEmail(){
const emailRegex = /^([a-z\d\.-]+)@([a-z\d-]+)\.([a-z]{2,3})(\.[a-z]{2,3})?$/;
const errorTxtEmail = document.querySelector(".error-txt.email");
if(!email.value.match(emailRegex)){
email.classList.add("error");
email.parentElement.classList.add("error");
if(email.value != ""){
errorTxtEmail.innerText = "Enter a valid email address";
}
else{
errorTxtEmail.innerText = "Enter Address can't be blank";
}
}
else{
email.classList.remove("error");
email.parentElement.classList.remove("error");
}
}
form.addEventListener("submit", (e) => {
e.preventDefault();
checkInputs();
if(!fullName.classList.contains("error") && !email.classList.contains("error") && !phone.classList.contains("error") &&
!subject.classList.contains("error") && !mess.classList.contains("error")){
sendEmail();
form.reset();
return false;
}
});