Robert J. Peterson
Robert J. Peterson

Reputation: 7

Parsley.js validation with post

I'm validating a form with the (excellently crafted) Parsley.js script, and there's one element I can't seem to get working.

When the user successfully fills out the form, I want two things to happen:

So here's my code as it stands:

$(document).ready(function(){
    // form
    var form = $('#formname');
    var success = $('.success');

    form.submit(function(){

        if($form.parsley('isValid') == true){
            $.post('https://www.xxxxx.custom.api.url', $(this).serialize(), function(){
                success.show(); $('input[type=text]').val('');
            },'json');
        }

        return false;
    });

});

If I remove the "if" line and its corresponding bracket, my two commands work perfectly -- the form information gets submitted, and the message displays.

But when I include the "if" line and its bracket, it doesn't.

I'm not the world's biggest expert on jQuery/Javascript/etc., so I'm sure I'm missing something obvious. Any ideas what's wrong with this line?

if($form.parsley('isValid') == true){

Thanks, everyone!

Upvotes: 0

Views: 4430

Answers (1)

guillaumepotier
guillaumepotier

Reputation: 7438

You have to validate form and not $form.

$form is undefined in your example above.

Try if(form.parsley('isValid') == true){} and see if something good happens.

Best

Upvotes: 1

Related Questions