KendoValidator需要实际上不需要的字段

时间:2022-11-30 11:26:50

I have a field that isn't required shown below in code. When I click save to post form data i get the kendo error message which should only validate that its a proper zipcode (it can be blank). It also shows the error message when i tab away from the field which is not what I'm looking for. Any help would be great, thanks!

我在代码中有一个不需要显示的字段。当我单击保存以发布表单数据时,我收到了kendo错误消息,该消息应该只验证其正确的邮政编码(它可以是空白的)。当我离开字段时,它还显示错误消息,这不是我正在寻找的。任何帮助都会很棒,谢谢!

HTML:

<input class="form-control " data-val="true" data-val-regex="Invalid Zip Code" data-val-regex-pattern="^\d{5}(-\d{4})?$" id="ZipCode" name="ZipCode" onkeyup="enableButton()" tabindex="4" type="text" value="">

JS:

$(document).ready(function () {
    $("#Form").kendoValidator();
    //$(".k-widget").removeClass("input-validation-error");
});

Model:

[Display(Name = "Zip Code")]
    [RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid Zip Code")]
    public string ZipCode { get; set; }

2 个解决方案

#1


1  

You need to set the Kendo Validator property 'validateOnBlur' to false (it defaults to true) to skip validation when the user tabs away.

您需要将Kendo Validator属性'validateOnBlur'设置为false(默认为true),以便在用户标签离开时跳过验证。

$(document).ready(function () {
    $("#Form").kendoValidator({
       validateOnBlur: false
     });
});

Also, I would use 'validationMessage' instead of 'data-val-regex', as well as 'pattern' instead of 'data-val-regex-pattern' in your input tag.

另外,我会在输入标记中使用'validationMessage'而不是'data-val-regex',以及'pattern'而不​​是'data-val-regex-pattern'。

Finally, if you leave out the 'required' attribute, your user will be allowed to leave the field empty.

最后,如果省略“required”属性,系统将允许您的用户将该字段留空。

I've created a Dojo with a working example of this: http://dojo.telerik.com/uPeQE

我已经创建了一个Dojo,其中包含一个工作示例:http://dojo.telerik.com/uPeQE

#2


1  

I found my answer. It actually wasn't a Kendo issue, but a regex issue.

我找到了答案。它实际上不是一个Kendo问题,而是一个正则表达式问题。

Regex makes a field required, but my regex data annotation wasn't accepting empty strings:

正则表达式需要一个字段,但我的正则表达式数据注释不接受空字符串:

    [RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid Zip Code")]

I added |^$ to the end of my regex and it solved my issue.

我添加了| ^ $到我的正则表达式的末尾,它解决了我的问题。

[RegularExpression(@"^\d{5}(-\d{4})?|^$", ErrorMessage = "Invalid Zip Code")]

#1


1  

You need to set the Kendo Validator property 'validateOnBlur' to false (it defaults to true) to skip validation when the user tabs away.

您需要将Kendo Validator属性'validateOnBlur'设置为false(默认为true),以便在用户标签离开时跳过验证。

$(document).ready(function () {
    $("#Form").kendoValidator({
       validateOnBlur: false
     });
});

Also, I would use 'validationMessage' instead of 'data-val-regex', as well as 'pattern' instead of 'data-val-regex-pattern' in your input tag.

另外,我会在输入标记中使用'validationMessage'而不是'data-val-regex',以及'pattern'而不​​是'data-val-regex-pattern'。

Finally, if you leave out the 'required' attribute, your user will be allowed to leave the field empty.

最后,如果省略“required”属性,系统将允许您的用户将该字段留空。

I've created a Dojo with a working example of this: http://dojo.telerik.com/uPeQE

我已经创建了一个Dojo,其中包含一个工作示例:http://dojo.telerik.com/uPeQE

#2


1  

I found my answer. It actually wasn't a Kendo issue, but a regex issue.

我找到了答案。它实际上不是一个Kendo问题,而是一个正则表达式问题。

Regex makes a field required, but my regex data annotation wasn't accepting empty strings:

正则表达式需要一个字段,但我的正则表达式数据注释不接受空字符串:

    [RegularExpression(@"^\d{5}(-\d{4})?$", ErrorMessage = "Invalid Zip Code")]

I added |^$ to the end of my regex and it solved my issue.

我添加了| ^ $到我的正则表达式的末尾,它解决了我的问题。

[RegularExpression(@"^\d{5}(-\d{4})?|^$", ErrorMessage = "Invalid Zip Code")]