JQuery custom javascript authentication mechanism

I am trying to test the availability of a subdomain using the jQuery validation mechanism using a custom function. The check runs correctly, but alertText is not displayed if the .ajax request returns 200. I have added Access-Control-Allow-Origin headers and can successfully complete the request 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> 
+6
source share
1 answer

The problem is that $.ajax is an asynchronous function and does not work that 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 this: your return applies only to [second function] not for validateDomain .

Looking at the jquery-validation-engine docs I don't see a way to do what you want.

0
source

All Articles