4中的点分隔客户端日期验证

4中的点分隔客户端日期验证

本文介绍了asp MVC 4中的点分隔客户端日期验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以几个小时以来,我一直在努力解决这个问题.我正在为我的日期输入字段创建本地化验证.我已经覆盖了默认的 jquery 日期验证器,并且该方法似乎工作正常.一切似乎都按计划进行,但 jquery 验证器不会让我发布表单.

So I've been banging my head to wall with this problem for hours. I'm creating localized validation for my date input fields. I've overwritten the default jquery date validator and the method seems to be working fine. Everything seems to go as planned but jquery validator just won't let me to post the form.

放置在自定义验证方法中的断点在萤火虫中被正确击中,并按预期返回 true.

Breakpoint placed in the custom validation method is hit properly in the firebug and it returns true as it's supposed to.

一旦我继续执行,带有红色矩形的验证错误字段就消失了,但验证摘要仍然显示 MVC 生成的属性错误.

Once I continue executing, the validation error fields with red rectangles are gone but validation summary still displays the MVC generated property errors.

代码如下:

tracker.validate.js

tracker.validate.js

// Replace dots so jq validator can understand what's going on
$(function () {
    $.validator.addMethod(
    "date",
    function (value, element) {
        var s = value;
        s = value.replace(/./g, '/');

        // Chrome requires tolocaledatestring conversion, otherwise just use the slashed format
        var d = new Date();
        return this.optional(element) || !/Invalid|NaN/.test(new Date(d.toLocaleDateString(value))) || !/Invalid|NaN/.test(new Date(s));
    },
    ""
    );
});

$.validator.unobtrusive.parse();

这是我的局部视图中的脚本引用,该表单所在的位置

Here are the script references in my partial view, where this form is located

<script src="@Url.Content("~/Scripts/jquery.validate.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/tracker.validate.js")" type="text/javascript"></script>

日期时间编辑器模板

@model DateTime


<input style="width:44%;display:inline;" type="text" name='@(ViewData.TemplateInfo.HtmlFieldPrefix).Day' value='@Model.Date.ToShortDateString()' class='date'/>
<input style="width:30%;display:inline;" type="text" name='@(ViewData.TemplateInfo.HtmlFieldPrefix).Time' value='@Model.ToShortTimeString()' onblur='formatTimeInput(this)'/>
@Html.HiddenFor(model => model)

日期选择器初始化:

$.datepicker.setDefaults($.datepicker.regional['fi']);

$('.date').datepicker({
    showOn: "button",
    dateFormat: "dd.mm.yy",
    buttonImage: '/content/images/calendarIcon.png',
    buttonImageOnly: true,
    constrainInput: false
});

是我遗漏了什么,还是有什么办法可以摆脱这些默认属性错误?

Is there something I'm missing or is there anyway to get rid of these default property errors?

推荐答案

显然还有其他脚本文件与验证冲突.我删除了这条线

Apparently there was some other script file conflicting with validations.I removed the line

<script src="@System.Web.Optimization.BundleTable.Bundles.ResolveBundleUrl("~/Scripts/js")"></script>

来自 _Layout.cshtml,它现在可以工作了.

From _Layout.cshtml and it's working now.

这篇关于asp MVC 4中的点分隔客户端日期验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 23:31