Asp.net mvc 4 使用Asp.NETMVC4应用程序中的Jquery验证插件引导datetime选择器验证日期和日期时间

Asp.net mvc 4 使用Asp.NETMVC4应用程序中的Jquery验证插件引导datetime选择器验证日期和日期时间,asp.net-mvc-4,jquery-validate,unobtrusive-validation,eonasdan-datetimepicker,Asp.net Mvc 4,Jquery Validate,Unobtrusive Validation,Eonasdan Datetimepicker,我目前正在开发一个MVC Asp.NET4.6WebApp,带有和 我在确认日期时遇到了一些问题 我的模型是这样的: public class PersonModel{ ... [Required] [Display(Name = "Date of Birth")] public DateTime? DateOfBirth { get; set; } ... } <div class="form-group">

我目前正在开发一个MVC Asp.NET4.6WebApp,带有和

我在确认日期时遇到了一些问题

  • 我的模型是这样的:

    public class PersonModel{
        ...
    
        [Required]
        [Display(Name = "Date of Birth")]
        public DateTime? DateOfBirth { get; set; }
    
        ...        
    }
    
    <div class="form-group">
        @Html.LabelFor(x => x.DateOfBirth):
        <span class="text-danger"><b>*</b></span>
        <div class="input-group datepicker">
            <span class="input-group-addon">
                <span class="glyphicon glyphicon-calendar"></span>
            </span>
            @Html.TextBoxFor(x => x.DateOfBirth, new {@class = "form-control", @data_date_format = "DD/MM/YYYY", @placeholder = "DD/MM/YYYY"})
        </div>
        @Html.ValidationMessageFor(x => x.DateOfBirth, "", new { @class = "text-danger" })
    </div>
    
    (function () {
        // overrides the jquery date validator method
        jQuery.validator.methods.date = function (value, element) {
            // We want to validate date and datetime
            var formats = ["DD/MM/YYYY", "DD/MM/YYYY HH:mm"];
            // Validate the date and return
            return moment(value, formats, true).isValid();
        };
    })(jQuery, moment);
    
  • 我的看法是:

    public class PersonModel{
        ...
    
        [Required]
        [Display(Name = "Date of Birth")]
        public DateTime? DateOfBirth { get; set; }
    
        ...        
    }
    
    <div class="form-group">
        @Html.LabelFor(x => x.DateOfBirth):
        <span class="text-danger"><b>*</b></span>
        <div class="input-group datepicker">
            <span class="input-group-addon">
                <span class="glyphicon glyphicon-calendar"></span>
            </span>
            @Html.TextBoxFor(x => x.DateOfBirth, new {@class = "form-control", @data_date_format = "DD/MM/YYYY", @placeholder = "DD/MM/YYYY"})
        </div>
        @Html.ValidationMessageFor(x => x.DateOfBirth, "", new { @class = "text-danger" })
    </div>
    
    (function () {
        // overrides the jquery date validator method
        jQuery.validator.methods.date = function (value, element) {
            // We want to validate date and datetime
            var formats = ["DD/MM/YYYY", "DD/MM/YYYY HH:mm"];
            // Validate the date and return
            return moment(value, formats, true).isValid();
        };
    })(jQuery, moment);
    
    使用
    jQuery.validator
    ,即使日期看起来不错,我也总是会遇到以下错误:

    我知道
    jQuery.validator
    与jQuery.ui.datepicker配合使用效果很好,但是如何使它与bootstrap.datetimepicker配合使用呢


您可以覆盖
Jquery.validator
插件的
date
方法:

(function () {
    // overrides the jquery date validator method
    jQuery.validator.methods.date = function (value, element) {
        // All dates are valid....
        return true;
    };
})(jQuery);
由于使用,您可以检查日期是否有效,如下所示:

public class PersonModel{
    ...

    [Required]
    [Display(Name = "Date of Birth")]
    public DateTime? DateOfBirth { get; set; }

    ...        
}
<div class="form-group">
    @Html.LabelFor(x => x.DateOfBirth):
    <span class="text-danger"><b>*</b></span>
    <div class="input-group datepicker">
        <span class="input-group-addon">
            <span class="glyphicon glyphicon-calendar"></span>
        </span>
        @Html.TextBoxFor(x => x.DateOfBirth, new {@class = "form-control", @data_date_format = "DD/MM/YYYY", @placeholder = "DD/MM/YYYY"})
    </div>
    @Html.ValidationMessageFor(x => x.DateOfBirth, "", new { @class = "text-danger" })
</div>
(function () {
    // overrides the jquery date validator method
    jQuery.validator.methods.date = function (value, element) {
        // We want to validate date and datetime
        var formats = ["DD/MM/YYYY", "DD/MM/YYYY HH:mm"];
        // Validate the date and return
        return moment(value, formats, true).isValid();
    };
})(jQuery, moment);

如果我想在同一个表单上同时使用validate和datetime呢。您正在覆盖“日期”方法。@Kai抱歉,我记不清了,这是一篇30年前的老文章。。。但是从第二个代码片段中,我可以看到一个有效的格式是datetime,所以应该能够验证日期和datetime,你试过了吗?对不起,我没有清楚地阅读代码,它确实工作得很好。非常感谢你。@Kai,不用担心。如果需要,还可以添加其他格式。这对我来说是可行的,但您可能希望根据您的用例调整格式