6

I am trying to validate the availability of a subdomain with the jQuery Validate Engine through a custom function. Validation appears to happen correctly but the alertText is not displayed if .ajax request returns 200. I have added Access-Control-Allow-Origin headers and can see the request completing successfully in my logs.

What am I doing wrong?

Javascript Function:

function validateDomain(field, rules, i, options) {
    var url = "https://" + field.val() + ".example.com/";
    $.ajax(url,
        {
            statusCode: {
                200: function() {
                    //alert('name exists already');
                    return options.allrules.validate2fields.alertText;
                }
            }
        });
}

Form Field:

    <label class="required" for="signup[subdomain]">Subdomain<span>*</span></label>
    <span>https://</span>
    <input id="signup[subdomain]" name="signup[subdomain]" class="field field validate[required,funcCall[validateDomain]]" type="text">
   <span>.example.com</span>
adamdehaven
  • 5,890
  • 10
  • 61
  • 84
mrbnetworks
  • 2,547
  • 3
  • 17
  • 10
  • can you add a error handler and see whether it is getting executed, my doubt is a parse error is occuring – Arun P Johny Mar 14 '13 at 13:32
  • 2
    looks like a duplicate of [How to return the response from an AJAX call from a function?](http://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-ajax-call-from-a-function) – Bergi Mar 14 '13 at 14:36
  • 1
    You've tagged this both [tag:jquery-validate] and [tag:jquery-validation-engine] and yet these are two totally different plugins. Which one are you really using? – Sparky Mar 14 '13 at 15:06
  • 1
    Sparky has edited the correct plugin. I am using jquery-validate-engine. @Bergi This is what I needed. Adding a callback for the request works. – mrbnetworks Mar 14 '13 at 16:52

1 Answers1

0

The problem is that $.ajax is a asynchronous function and don't work this way.

function validateDomain(field, rules, i, options) {
    var url = "https://" + field.val() + ".example.com/";
    $.ajax(url,
        {
            statusCode: {
                200: function() { // [ second function ]
                    return options.allrules.validate2fields.alertText;
                }
            }
        });
}

The problem is: your return is only for the [second function] not for validateDomain.

Looking at the docs of jquery-validation-engine I can't see a way of doing what you want.

danilopopeye
  • 9,610
  • 1
  • 23
  • 31