Sophivorus
Sophivorus

Reputation: 3083

jQuery on submit only fires once

I've got a form that is loaded dynamically, with a textarea that I must check for spam before submitting. So I wrote something like this:

$(document).ready(function(){
    $('form').live('submit',function(){
        if ( $('form textarea').val().match(/https?:\/\/|www\.|\.com/) ) {
            return false;
        }
        return true;
    })
});

And it works fine, the first time. However, if I click on the submit button again, the form is submitted without going through the validation. There are some related questions in SO already, but I've tried their answers and can't seem to make it work. I tried for example attaching the listener to the document rather than the form, and using the on method rather than live, but with no luck yet. Any help is appreciated!

Upvotes: 0

Views: 581

Answers (1)

Musa
Musa

Reputation: 97672

The form in $('form textarea') may not be the same form that triggered the submit event, to use the form that triggered the event use this

$('form').live('submit',function(){
    if ( $('textarea', this).val().match(/https?:\/\/|www\.|\.com/) ) {
        return false;
    }
    return true;
})

Upvotes: 3

Related Questions