Ihor Tkachuk
Ihor Tkachuk

Reputation: 1118

Contact form 7 call submit event in jQuery

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?

Upvotes: 2

Views: 21781

Answers (3)

Mohammed Abdelhay
Mohammed Abdelhay

Reputation: 13

you may preventDefault if the input is empty like so:

$('.wpcf7-submit').on('click', function (e) {
var testInput = $('input[name="test"]').val();

if (testInput === '') {
    e.preventDefault();

}

});

OR:

$('.wpcf7-submit').on('click', function (e) {
e.preventDefault();

var testInput = $('input[name="test"]').val();

if (testInput != '') {
    $('#form-id').submit();
}

});

Hope it will help.

Upvotes: 1

user2374888
user2374888

Reputation: 11

This should work :

$('.wpcf7-submit').submit();

Upvotes: 1

Frnak
Frnak

Reputation: 6792

First check if it's empty, then preventDefault. It will then submit otherwise

$('.wpcf7-submit').on('click', function (e) {
    var testInput = $('input[name="test"]').val();

    if (testInput === '') {
        e.preventDefault();
    }
});

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:

$('#myForm').on('submit', function() {
  ....
}

Instead of listening to the click

Upvotes: 5

Related Questions