How to prevent sending the request?

There is such a request.
let form = document.querySelector('#form');

form.addEventListener('submit', function(evt) {
evt.preventDefault();

 let formData = {
 name: document.querySelector('input[name="name"]').value
 phone: document.querySelector('input[name="phone"]').value
 email: document.querySelector('input[name="email"]').value
 textarea: document.querySelector('textarea[name=textarea]').value
};

 var request = new XMLHttpRequest();

 request.addEventListener('load', function() {
 alert('Your message was sent successfully!');
 form.innerHTML = '<h2 class="form__height">Thank you!</h2>';
});

 request.open('POST', './mail.php', true);
 request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
 request.send('name=' + encodeURIComponent(formData.name) + '&phone=' + encodeURIComponent(formData.phone) + '&email=' + encodeURIComponent(formData.email) + '&textarea=' + encodeURIComponent(formData.textarea) );
});


Want to do spam protection through the creation of a hidden empty field if the hidden field is empty, the request is sent, and if the hidden field is filled in, the request is not sent. Tell me, how do this?
April 7th 20 at 10:58
1 answer
April 7th 20 at 11:00
Solution
form.addEventListener('submit', function(evt) {
evt.preventDefault();

 // do nothing
 if (document.querySelector('#hidden').value) return

 let formData = {
 name: document.querySelector('input[name="name"]').value
 phone: document.querySelector('input[name="phone"]').value
 email: document.querySelector('input[name="email"]').value
 textarea: document.querySelector('textarea[name=textarea]').value
};

 var request = new XMLHttpRequest();
Once again thank you - rocio.Stiedema commented on April 7th 20 at 11:03

Find more questions by tags JavaScript