jQuery的DD / MM / YYYY日期格式验证 [英] Jquery dd/MM/yyyy date format validation

查看:284
本文介绍了jQuery的DD / MM / YYYY日期格式验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用我的应用程序的默认ASP.NET MVC 4验证包。鉴于我在DD / MM / YYYY和jQuery验证的格式日期字段无法验证该格式。然后,我添加下面的code来覆盖默认行为。

I using default ASP.NET MVC 4 validation bundle in my application. In view I have a date field in the format of "dd/MM/yyyy" and jquery validation failed to validate the format. Then I added the below code to override the default behavior.

$(function () {
        $.validator.methods.date = function (value, element) {
            Globalize.culture("en-GB");
            // you can alternatively pass the culture to parseDate instead of
            // setting the culture above, like so:
           // parseDate(value, null, "en-AU")
            return this.optional(element) || Globalize.parseDate(value) !== null;
        }
    });

然后将日期验证问题得到了解决,现在我的应用程序不会触发客户端验证,但服务器端验证。这可能是对这种行为的原因是什么?

Then the date validation issue was resolved, and now my application does not fire client side validation but the server side validation. what could be the reason for this behavior?

推荐答案

据失踪globalize.js文件。我修改了code如下现在工作的罚款。

It was missing the globalize.js file. i modified the code as below and now working fine.

https://github.com/jquery/globalize

包括相关的.js文件的引用(您也可以使用的NuGet包安装为好,这是我用过的选项。)

include references to relevant .js files (You can also install it with NuGet packages as well, which is the option I have used.)

<script src="~/Scripts/globalize/globalize.js"></script>
<script src="~/Scripts/globalize/cultures/globalize.culture.en-GB.js"></script>

$(document).ready(function () {
        $.culture = Globalize.culture("en-GB");
        $.validator.methods.date = function (value, element) {
            //This is not ideal but Chrome passes dates through in ISO1901 format regardless of locale 
            //and despite displaying in the specified format.

            return this.optional(element)
                || Globalize.parseDate(value, "dd/MM/yyyy", "en-GB")
                || Globalize.parseDate(value, "yyyy-mm-dd");
        }
    });

这篇关于jQuery的DD / MM / YYYY日期格式验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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