0

I'm using the .validity jquery plugin (validity.thatscaptaintoyou.com/), and am trying to also use a .submit, but I'm not sure how to prevent both from executing simultaneously. Basically, I don't want to .submit to execute unless the validity passes.

Validity Check:

$("#myform").validity(function() {
    $("#first_name").require();
    $("#last_name").require();
    $("#email").require().match('email', 'Please enter a valid e-mail address');
});

Sample .submit

$('#myform').submit(function(event) {//Execute only if validity passed}

2 Answers2

0

you could try using their custom mode (function(){

// We'll decide to install our custom output mode under the name 'custom':
 $.validity.outputs.custom = {


    end:function(results) { 


        if (results.valid) {
            $('#myform').submit(function(event) {//Execute only if validity passed});
        }

    }
}
})();

// Now enable the output mode we just installed.
$.validity.setup({ outputMode:'custom' });
Daniel Ward
  • 165
  • 8
  • I tried, but it just submits without any validation through validity, or running the .submit within the "end:". I also tried adding the raise: and raiseAggregate: options, but that resulted in the same thing where the form submits regardless. – dgraver Oct 02 '13 at 17:13
0

This worked for me:

      function validateMyForm() {

    // Start validation:
    $.validity.start();

    // Validator methods go here:
    $("#first_name").require();
    $("#last_name").require();
    $("#email").require().match('email', 'Please enter a valid e-mail address');

    // All of the validator methods have been called:
    // End the validation session:
    var result = $.validity.end();

    // Return whether it's okay to proceed:
    return result.valid;
}

// Submit handler for /purchase
$('#myform').submit(function(event) {

//Execute only if validity passed
        if (validateMyForm()) {
               //Submit form or do whatever

    }else{
               //Return False so form doesn't actually submit
        return false;
    }
});