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>
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.