igogra
igogra

Reputation: 465

form submit after preventDefault not working until submit button is clicked a second time

I have a form where I validate some fields by means of jQuery and after checking that everything is correct I want to submit the form. So first I prevent the form to be submit when the submit button is clicked, I perform the validations and finally if everything is correct I submit the form. But my form is not submitted until the submit button is clicked twice. How can I solve it?

$('#form1').submit(function(e) {
    e.preventDefault();
    ... // Validation actions
    if (form_ok) {
       $(this).unbind('submit').submit();
    }
});

Thanks

Upvotes: 1

Views: 630

Answers (1)

Hemal
Hemal

Reputation: 3760

WORKING FIDDLE

CODE

$(document).ready(function(){
    $('#form1').submit(function(e) {
        var form_ok=$(":text").val().length>0;

        if(!form_ok)
        {
            e.preventDefault();
        }

    });
});

HTML

<form id="form1">
    <input type="text" />
    <input type="submit" value="Submit" />

</form>

Upvotes: 1

Related Questions