开发者

validity plugin jquery problem?

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开发者_JS百科(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


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


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
    }
});


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) 
});


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜