编程语言
首页 > 编程语言> > c#-ASP.NET MVC3:验证日期时间和自定义验证消息

c#-ASP.NET MVC3:验证日期时间和自定义验证消息

作者:互联网

>如何防止用户输入datetimepicker(文本字段),
但允许他们使用datetimepicker.
>如何验证一个日期晚于另一个日期,并像ASP.Net MVC3一样内嵌显示(并防止提交). javascript并没有那么多地检查它的问题,它如何显示此内联消息并阻止提交,并与其他验证联系起来.

到目前为止,我有

            $("#Save").click(function (e) {
                if (  $('#EndTime').val() < $('#StartTime').val() )
                    alert("AHRR"); // Should add an inline message and NOT submit.
            });

解决方法:

我建议使用jQuery’s datepicker在客户端进行日期范围约束.您需要禁用输入元素< input readonly />.我最近实现了一个对我有用的日期范围约束.它将结束日期限制在开始日期的90天内

仅供参考,我停止使用startElement和endElement作为测试,再也没有回过头来,因为该代码当前未被重用.

// ============================================================================
// FUNCTION: InitializeDates(startElement, endElement)
// PARAMETERS
// ----------
// startElement:    The element which will be initialized as the start-date 
//                  datepicker.
//
// endElement:      The element which will be initialized as the start-date 
//                  datepicker.
//
// DESCRIPTION
// -----------
// InitializeDates updates the start and end dates for non-employees.  It 
// creates a date-picker object on both fields and then constrains the end 
// end date:
// * No date selections available prior to the start date
// * No date selections available after 90 days beyond the start date.
// ----------------------------------------------------------------------------
function InitializeDates(startElement, endElement)
{
    $("#f-start-date").datepicker({
        showOn: "button",
        buttonImage: "images/calendar.png",
        buttonImageOnly: true,
        onSelect: function(dateText, inst) { StartDateSelected(dateText, inst) }
    });

    $("#f-end-date").datepicker({
        showOn: "button",
        buttonImage: "images/calendar.png",
        buttonImageOnly: true,
        numberOfMonths: 3
    });

    $("#f-start-date").val('');
    $("#f-end-date").val('');
}

// ============================================================================
// FUNCTION: StartDateSelected(dateText, endElement)
// PARAMETERS
// ----------
// dateText:    The dateText passed from jQuery.datepicker.onSelect
// inst:        The instpassed from jQuery.datepicker.onSelect//
//
// DESCRIPTION
// -----------
// Updates the end-date maxDate and minDate fields to 91 dates from the selected
// start date.
// ---------------------------------------------------------------------------
function StartDateSelected(dateText, inst)
{
    var second = 1000;
    var minute =second * 60;
    var hour = minute * 60;
    var day = hour * 24;

    // The datepicker updates maxDate and minDate based on today's date.  I've
    // got to math out dates so that the maxDate is actually 91 days from the
    // selected date.  
    var todaysDate = new Date();
    var selectedDate = new Date(dateText);          
    var duration = Math.floor((selectedDate - todaysDate) / day) + 91;

    $("#f-end-date").datepicker("option", "minDate", selectedDate);
    $("#f-end-date").datepicker("option", "maxDate", duration);
    $("#f-end-date").val('');
}

标签:validation,javascript,c,asp-net-mvc-3,datetimepicker
来源: https://codeday.me/bug/20191202/2085287.html