put  'return result' inside of the success method.  Everything outside
of the ajax call is done sequentially, so its returning before the
ajax call comes back.  Actually, you may need to put the ajax call
outside of validator.addmethod and call validator.addmethod inside of
the success.

On Nov 27, 8:29 am, Fledder <[EMAIL PROTECTED]> wrote:
> hi,
>
> I'm using the jQuery Validation plugin for a big project and I'm
> loving it. I am however getting desperate trying to solve a particular
> issue. I've hunted for a solution for days now and tried everything I
> could think of, but it seems I coded myself into a deadlock. I'm
> hoping you can have a quick look at my code, it's only a little bit :)
>
> I'm developing a user registration form with quite some advanced
> validation. So far it works just fine, apart from one thing: remotely
> checking if the username does not exist already. Here's my code:
>
> // extend the validation plugin to do remote username dupe checking
> jQuery.validator.addMethod('usernameCheck', function(username) {
>     var postURL = "<?= $basepath; ?>user/json_username_check/" +
> username + "/" + new Date().getTime();
>     $.ajax({
>         type: "POST",
>         url: postURL,
>         success: function(msg) {
>             result = (msg=='TRUE') ? true : false;
>         }
>     });
>     return result;
>
> }, '');
>
> The problem is that I cannot return the result of the Ajax call back
> to the Validator. The Ajax call itself works fine, I can see that
> using Firebug. I've been advised to not use this low level jQuery Ajax
> call, but to use the "remote" option of the validator plugin instead.
>
> However, the reason I'm not using the "remote" selector from the
> Validation plugin is that my backend (Code Igniter) does not accept
> params in the format ?name=value. It will block all those. Code
> Igniter expects this: /name/value. It is possible to tell Code Igniter
> to use query strings, but that will break my entire application.
> Therefore, I am forced to use the $.ajax call.
>
> In my research somewhere I found somebody saying that asynchronous
> calls inside an addmethod do not work. I figured explicitly setting
> the async = false in the Ajax request (not shown above) would help,
> but it doesn't. The Ajax request itself works fine but I have no way
> to return the result of it to the Validator plugin. With "remote" not
> being an option and "AddMethod" not working, I have no idea what to do
> next. I don't mean to sound lazy, but rewriting or hacking the plugin
> itself beats my purpose of reusing and saving time.
>
> I've been so productive in producing forms thanks to this plugin but
> spending so much time on this little part got me quite depressed. I
> hope you can give me some advise, I would be very grateful.
>
> Thanks!
>
> Ferdy

Reply via email to