简体   繁体   English

jQuery验证dd / mm / yyyy格式的日期

[英]jQuery validation for dates in dd/mm/yyyy format

I'm working on an ASP.NET MVC 5 project. 我正在研究ASP.NET MVC 5项目。 I have two text boxes in a view like so ... 我在这样的视图中有两个文本框...

@Html.TextBox("earliestDate", Model.Checklists.EarliestDate)
@Html.TextBox("latestDate", Model.Checklists.LatestDate)

I'm entering the dates "01/03/2014" (1st March 2014) and "31/03/2014" (31st March 2014). 我输入的日期为“ 01/03/2014”(2014年3月1日)和“ 31/03/2014”(2014年3月31日)。 When I place a breakpoint in the controller, I can see that in the controller earliestDate has the value "03/01/2014" (the day and month I entered have been swapped around) while latestDate is null. 当我在控制器中放置断点时,我可以看到在控制器中earlyestDate的值为“ 03/01/2014”(我输入的日期和月份已经交换了),而latestDate为空。 The code works as desired if I enter the dates in yyyy-MM-dd format. 如果我以yyyy-MM-dd格式输入日期,则代码可以按要求工作。

I followed the advice in this article. 我跟着建议文章。

I have installed "globalize.js" and added the code from the article to my view. 我已经安装了“ globalize.js”,并将文章中的代码添加到了我的视图中。 Nothing changed, I still got the results described above. 什么都没改变,我仍然得到了上述结果。 So I tried modifying the code to specify the desired culture explicitly like so, old code commented out, my new code beneath it ... 因此,我尝试修改代码以明确指定所需的区域性,例如,旧代码被注释掉,我的新代码在下面...

@*<script src="~/Scripts/globalize/cultures/globalize.culture.@(System.Threading.Thread.CurrentThread.CurrentCulture.Name).js"></script>*@
<script src="~/Scripts/globalize/cultures/globalize.culture.en-IE.js"></script>

and ... 还有...

//Globalize.culture('@(System.Threading.Thread.CurrentThread.CurrentCulture.Name)');

Globalize.culture('en-IE'); Globalize.culture('en-IE');

This still didn't make any difference. 这仍然没有任何区别。 So I tried explicitly adding the desired format to the validator.methods.date function like so ... 所以我尝试将所需的格式显式添加到validator.methods.date函数中,如下所示...

$.validator.methods.date = function (value, element) {
    return this.optional(element) ||
        Globalize.parseDate(value) ||
        Globalize.parseDate(value, "dd-MM-yyyy") || // new code
        Globalize.parseDate(value, "yyyy-MM-dd");
}

Still no difference. 仍然没有区别。 What am I missing? 我想念什么?

For reference, here is the complete script section from the view including the above modifications ... 作为参考,这是该视图中完整的脚本部分,包括上述修改...

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")

    <script src="~/Scripts/globalize/globalize.js"></script>
    @*<script src="~/Scripts/globalize/cultures/globalize.culture.@(System.Threading.Thread.CurrentThread.CurrentCulture.Name).js"></script>*@
    <script src="~/Scripts/globalize/cultures/globalize.culture.en-IE.js"></script>
    <script>
    $.validator.methods.number = function (value, element) {
        return this.optional(element) ||
            !isNaN(Globalize.parseFloat(value));
    }
    $(document).ready(function () {
        //Globalize.culture('@(System.Threading.Thread.CurrentThread.CurrentCulture.Name)');
        Globalize.culture('en-IE');
    });
    </script>
    <script>
        jQuery.extend(jQuery.validator.methods, {
            range: function (value, element, param) {
                //Use the Globalization plugin to parse the value
                var val = Globalize.parseFloat(value);
                return this.optional(element) || (
                    val >= param[0] && val <= param[1]);
            }
        });
        $.validator.methods.date = function (value, element) {
            return this.optional(element) ||
                Globalize.parseDate(value) ||
                Globalize.parseDate(value, "dd/MM/yyyy") || // new code
                Globalize.parseDate(value, "yyyy-MM-dd");
        }
    </script>
}

What is in your jQueryval Bundle? 您的jQueryval捆绑包中有什么?

I'd have the same problem and I solve it adding the script ~/Scripts/globalize/globalize.js 我会有同样的问题,我通过添加脚本~/Scripts/globalize/globalize.js

This script solves the date problem but add a new problem with number validation. 该脚本解决了日期问题,但通过数字验证添加了新问题。 Without this script, date validation is wrong and number validation ok... 如果没有此脚本,则日期验证错误,数字验证正常。

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

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