jQuery validation engine custom javascript validation
Asked Answered
K

1

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>
Kuehnel answered 14/3, 2013 at 13:14 Comment(4)
can you add a error handler and see whether it is getting executed, my doubt is a parse error is occuringRozalin
looks like a duplicate of How to return the response from an AJAX call from a function?Barta
You've tagged this both jquery-validate and jquery-validation-engine and yet these are two totally different plugins. Which one are you really using?Tasman
Sparky has edited the correct plugin. I am using jquery-validate-engine. @Barta This is what I needed. Adding a callback for the request works.Kuehnel
S
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.

Styria answered 9/4, 2013 at 21:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.