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

时间:2023-01-14 14:32:59

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?

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

6 个解决方案

#1


11  

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

为我找到一个更简单的解决方案:在jQuery Validate中,日期格式dd / mm / yyyy的客户端验证失败

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

#2


6  

You can use dateITA from additional methods.

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

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

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

I hope it works.

我希望它有效。

#3


2  

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

#4


1  

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

#5


1  

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用于您的服装规则。

#6


0  

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]
}

#1


11  

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

为我找到一个更简单的解决方案:在jQuery Validate中,日期格式dd / mm / yyyy的客户端验证失败

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

#2


6  

You can use dateITA from additional methods.

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

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

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

I hope it works.

我希望它有效。

#3


2  

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

#4


1  

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

#5


1  

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用于您的服装规则。

#6


0  

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]
}