-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail.js
78 lines (62 loc) · 2.36 KB
/
mail.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
const firebaseConfig = {
apiKey: "AIzaSyCMqLttSQT9iR9WEAnEGD-6EWCfwtHfBQI",
authDomain: "my-first-projectsrv.firebaseapp.com",
databaseURL: "https://my-first-projectsrv-default-rtdb.firebaseio.com",
projectId: "my-first-projectsrv",
storageBucket: "my-first-projectsrv.appspot.com",
messagingSenderId: "725600835240",
appId: "1:725600835240:web:1e93f7076e0fe257a7549d"
};
// initialize firebase
firebase.initializeApp(firebaseConfig);
// reference your database
var contactFormDB = firebase.database().ref("contactForm");
document.getElementById("contactForm").addEventListener("submit", submitForm);
document.getElementById("confirmPassword").addEventListener("input", validatePasswords);
function submitForm(e) {
e.preventDefault();
var name = getElementVal("name");
var emailid = getElementVal("emailid");
var phone = getElementVal("phone");
var password = getElementVal("password");
var confirmPassword = getElementVal("confirmPassword");
var msgContent = getElementVal("msgContent");
// Validation for password and confirm password
if(password !== confirmPassword) {
alert("Passwords do not match. Please try again.");
return;
}
saveMessages(name, emailid, phone, password, msgContent);
// enable alert
document.querySelector(".alert").style.display = "block";
// remove the alert
setTimeout(() => {
document.querySelector(".alert").style.display = "none";
}, 3000);
// reset the form
document.getElementById("contactForm").reset();
document.querySelector(".tick").style.display = "none"; // hide tick after form reset
}
const saveMessages = (name, emailid, phone, password, msgContent) => {
var newContactForm = contactFormDB.push();
newContactForm.set({
name: name,
emailid: emailid,
phone: phone,
password: password,
msgContent: msgContent,
});
};
const getElementVal = (id) => {
return document.getElementById(id).value;
};
function validatePasswords() {
var password = document.getElementById("password").value;
var confirmPassword = document.getElementById("confirmPassword").value;
var tick = document.querySelector(".tick");
if (password === confirmPassword && confirmPassword !== "") {
tick.style.display = "block";
} else {
tick.style.display = "none";
}
}