简体   繁体   English

bassistance jquery验证插件 - 依赖性不匹配

[英]bassistance jquery validation plugin - dependency-mismatch

I have searched stack overflow and can't find an answer to my question. 我搜索了堆栈溢出,无法找到我的问题的答案。

I am using bassistance jquery validation plugin , and it is working well all except for one particular section, custom methods. 我正在使用bassistance jquery验证插件 ,它除了一个特定的部分,自定义方法以外都运行良好。

The methods are working it is just that calling $('selector').valid() is telling me the element is invalid, even though it should be valid. 方法正在工作只是调用$('selector')。valid()告诉我元素是无效的,即使它应该是有效的。

This is due to the this.optional(element) returning "dependency-mismatch". 这是由于this.optional(元素)返回“dependency-mismatch”。

My question is: 我的问题是:

What does dependency-mismatch mean when checking for optional on an element? 在检查元素上的可选项时,依赖项不匹配意味着什么?

Does dependency mean that it is optional, but has other dependencies? 依赖是否意味着它是可选的,但有其他依赖性?

So should my custom method check the optional field for "dependency-mismatch"?? 那么我的自定义方法是否应检查“dependency-mismatch”的可选字段?

I am unclear what it is suppose to return and why. 我不清楚回归的原因是什么。

should my custom method also return dependency-mismatch? 我的自定义方法是否也会返回依赖项不匹配? or should I deal with it?? 或者我应该处理它?

This is my custom method: 这是我的自定义方法:

$.validator.addMethod(
        "ukphonenumber",
        function (value, element, validate) {

            if (validate) {
                var regexp = "\\s*\\(?((\\+0?44)?\\)?[ \\-]?(\\(0\\))|0)((20[7,8]{1}\\)?[ \-]?[1-9]{1}[0-9]{2}[ \\-]?[0-9]{4})|([1-8]{1}[0-9]{3}\\)?[ \\-]?[1-9]{1}[0-9]{2}[ \\-]?[0-9]{3}))\\s*";
                var re = new RegExp(regexp);
                return this.optional(element) || re.test(value);
            }
            else {
                return true;
            }
        },
        "Please check your input."
    );

And this adding the rule to my input: 这将规则添加到我的输入中:

$("#<%=txtTelephoneNumber.ID %>").rules("add", {
            required: false,
            ukphonenumber: true,
            messages: {
                required: "Please enter your telephone number",
                ukphonenumber: "Your telephone number is invalid"
            }
        });

I also emailed the developer of the library and got this response: Which solved the problem. 我还通过电子邮件向图书馆的开发人员发送了回复:这解决了问题。

is there any reason to use required:false instead of required:true? 是否有任何理由使用required:false而不是required:true? If the field isn't required, leave out the required rule instead of setting it to false. 如果不需要该字段,请省略所需规则,而不是将其设置为false。

You could also try and leave out the call to this.optional(element) within your custom method. 您也可以尝试在自定义方法中省略对this.optional(元素)的调用。

Regards Jörn 关心Jörn

this works: 这工作:

return (this.optional(element) != false) || re.test(value);

Dependency mismatch with Jquery Validate plugin 与Jquery Validate插件的依赖性不匹配

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

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