在asp MVC 4中分开客户端日期验证 [英] Dot separated clientside date validation in asp MVC 4

查看:120
本文介绍了在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.

放置在自定义验证方法中的断点在firebug中正确打击,它将按原样返回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>

日期时间editortemplate

Datetime editortemplate

@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初始化:

Datepicker initialization:

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

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

有没有什么我缺少或有没有办法摆脱这些默认属性错误? / p>

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中分开客户端日期验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆