Jquery dd/MM/yyyy 日期格式验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20852745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 01:21:05  来源:igfitidea点击:

Jquery dd/MM/yyyy date format validation

jqueryasp.net-mvcvalidationasp.net-mvc-4date

提问by chamara

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.

我在我的应用程序中使用默认的 ASP.NET MVC 4 验证包。鉴于我有一个格式为“dd/MM/yyyy”的日期字段,jquery验证未能验证该格式。然后我添加了下面的代码来覆盖默认行为。

$(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?

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

采纳答案by chamara

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

它缺少 globalize.js 文件。我修改了下面的代码,现在工作正常。

download the files from https://github.com/jquery/globalize

https://github.com/jquery/globalize下载文件

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

包含对相关 .js 文件的引用(您也可以使用 NuGet 包安装它,这是我使用的选项。)

<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");
        }
    });

回答by Dai Bok

Found a simpler solution for me on see : Clientside validation fails for date format dd/mm/yyyy in jQuery Validate

为我找到了一个更简单的解决方案: Clientside validation failed for date format dd/mm/yyyy in jQuery Validate

Like it because I don't need new third party scripts or any extra plugins

喜欢它,因为我不需要新的第三方脚本或任何额外的插件

Basically overwrite the validation method like this:

基本上像这样覆盖验证方法:

$(document).ready(function () {
    $.validator.methods.date = function(value, element) {
        return this.optional(element) || parseDate(value, "dd-MM-yyyy") !== null;
    };
});

回答by jcamelis

You can use dateITA from additional methods.

您可以从其他方法中使用 dateITA。

http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/additional-methods.js

http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.0/additional-methods.js

$("#myform").validate({
    rules: {
        dateITA: true
    }
})

I hope it works.

我希望它有效。

回答by Kwex

Thanks Dai. Had to use something similar from date.js library. See below:

谢谢戴。不得不使用 date.js 库中类似的东西。见下文:

$.validator.methods.date = function(value, element) { 
  return this.optional(element) || Date.parseExact(value, "dd-MM-yyyy"); 
};

回答by Kamesh Jungi

You can use following code to validate :

您可以使用以下代码进行验证:

    jQuery.validator.addMethod("validdate", function(value, element) {
       return this.optional(element) ||  /^\d{1,2}\/\d{1,2}\/\d{4}$/.test(value);
    }, "Please enter valid date.");

Kindly use $ or jQuery as per your code. Here you just required to add rule in jQuery code and let use validdate to your costume rule.

请根据您的代码使用 $ 或 jQuery。在这里,您只需要在 jQuery 代码中添加规则,并在您的服装规则中使用 validdate。

回答by Facax

I prefer to format in MM/dd/yyyy and use the same validation of jQuery. This works perfect if you overrite the jQuery date validation.

我更喜欢在 MM/dd/yyyy 中格式化并使用相同的 jQuery 验证。如果您覆盖 jQuery 日期验证,这将非常有效。

$.validator.methods.date = function (value, element) {
    value = dateValue(value);
    return this.optional(element) || !/Invalid|NaN/.test(new Date(value).toString());;
}

function dateValue(value) {
    var date = value.split("/");
    return date[2] + "/" + date[1] + "/" + date[0]
}