I have a simple contact form and in the end I added a custom field:
<input type="text" name="test" id="test" class="form-control">
I need to check field is empty and then submit form:
$('.wpcf7-submit').on('click', function (e) {
e.preventDefault();
var testInput = $('input[name="test"]').val();
if (testInput != '') {
// How to submit form here?
}
});
How to do it?
First check if it’s empty, then preventDefault. It will then submit otherwise
EDIT:
You should also consider that people do not submit by pressing submit, but by pressing enter – therefore you should proably do something like this:
Instead of listening to the click
This should work :
you may preventDefault if the input is empty like so:
});
OR:
});
Hope it will help.