简体   繁体   English

jQuery验证引擎自定义javascript验证

[英]jQuery validation engine custom javascript validation

I am trying to validate the availability of a subdomain with the jQuery Validate Engine through a custom function. 我试图通过自定义函数验证jQuery验证引擎的子域的可用性。 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. 验证似乎正确发生,但如果.ajax请求返回200,则不会显示alertText。我添加了Access-Control-Allow-Origin标头,可以在我的日志中看到请求成功完成。

What am I doing wrong? 我究竟做错了什么?

Javascript Function: Javascript功能:

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. 问题是$.ajax是一个异步函数,不能以这种方式工作。

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 . 问题是:您的返回仅适用于[second function]而不适用于validateDomain

Looking at the docs of jquery-validation-engine I can't see a way of doing what you want. 看看jquery-validation-engine的文档,我看不到做你想做的事情的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM