-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.js
44 lines (34 loc) · 1.47 KB
/
contact.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
document.addEventListener('DOMContentLoaded', () => {
const recaptchaElement = document.querySelector('.g-recaptcha');
function updateRecaptchaSize() {
if(window.innerWidth <= 530) {
recaptchaElement.setAttribute('data-size','compact');
} else {
recaptchaElement.setAttribute('data-size','normal');
}
}
// calling the function on page load
updateRecaptchaSize();
const submitButton = document.getElementById('submitBtn');
const contactForm = document.getElementById('contactForm');
// check if the device is mobile
const isMobile = /iPhone|iPad|iPod|Android/i.test(navigator.userAgent);
// define the event type based on the device type
const eventType = isMobile ? 'touchstart' : 'click';
submitButton.addEventListener(eventType, function(event) {
event.preventDefault();
const name = document.getElementById('name').value;
const email = document.getElementById('email').value;
const message = document.getElementById('message').value;
if (name !== '' && email !== '' && message !== '') {
let recaptchaResponse = grecaptcha.getResponse();
if (recaptchaResponse !== '') {
contactForm.submit();
} else {
alert('Please complete the reCAPTCHA before submitting again.');
}
} else {
alert('Please fill out all fields before submitting again.');
}
});
});