2

I'm using the validity plugin to validate a form. It's working fine but is it possible to execute a callback function if the validity function succeeded like the following

$("form").validity(function(){
    $("#place").require().range(3, 50);
    $("#location").require().greaterThan(4) 
}, function(){ /* Here to put the call back code */ });

because i want to to run some code if the form validation successes , Any idea would be appreciated

Charles
  • 50,943
  • 13
  • 104
  • 142
osos
  • 2,103
  • 5
  • 28
  • 42

4 Answers4

0

According to the docs it's not easily possible.

However, you might be able to write a custom "output mode" that executes your callback: http://validity.thatscaptaintoyou.com/Demos/index.htm#CustomOutputMode

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
0

You could use the validate.start / validate.end method -> http://validity.thatscaptaintoyou.com/Demos/index.htm#UsingValidityWithAjax

it would look something like this (untested!):

function validateMyAjaxInputs() {
    $.validity.start();
     $("#place").require().range(3, 50);
    $("#location").require().greaterThan(4) 
    var result = $.validity.end();
    return result.valid;
}

$("form").validity(function(){

    if (validateMyAjaxInputs()) {
        // Do ajax request here
    }
});
Christophe
  • 4,798
  • 5
  • 41
  • 83
0

Okay, validity supports pluggable "output modules".

Here's one (admittedly hacky and untested one) that will call your callback function. Just append the code to jquery.validity.outputs.js or put it into your own file that you load after validity.

(function($) {
    $.validity.outputs.callback = {
        start:function() {
            buffer = [];
        },

        end:function(results) {
            $.validity.settings.callback(results);
        },

        raise:function($obj, msg) {
            buffer.push(msg);
        },

        raiseAggregate:function($obj, msg) {
            this.raise($obj, msg);
        },

        container:function() {}
    };
})(jQuery);

Usage:

$.validity.setup({
    outputMode:"callback",
    callback:function(){ /* your callback */ }
});

$("form").validity(function(){
    $("#place").require().range(3, 50);
    $("#location").require().greaterThan(4) 
});
vzwick
  • 11,008
  • 5
  • 43
  • 63
0

Normally you can do this kind of stuff like this:

var IsValid = function(){
 $.validity.start();
 $("#place").require().range(3, 50);
 $("#location").require().greaterThan(4) 
 var result = $.validity.end();
 return result;
}

if (IsValid()) { 
 //You callBack or whatewer
}

Of course using this pattern you are able to implement you js object which would be more convienent to use.

Saulius
  • 1,736
  • 20
  • 29
  • I think that that won't work because according to the doc validity start and end will be used for ajax calls, and this code will run also the form is not validated – osos Sep 26 '11 at 10:06
  • This code is working. You just call validation function before ajax (update). – Saulius Sep 26 '11 at 11:36