0

我正在尝试使用此 Kendo演示修改我的示例应用程序

在此处输入图像描述

<div class="form-group">
    @Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2 k-label" })
    <div class="col-md-10">    
        @(Html.Kendo().DatePicker().Name("StartDate").Value(Model.StartDate).HtmlAttributes(new { style = "width:250px"}).Events(e => e.Change("startChange"))) 

        @Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
    </div>
</div>

<div class="form-group">
    @Html.LabelFor(model => model.EndDate, htmlAttributes: new { @class = "control-label col-md-2 k-label" })
    <div class="col-md-10">
        @(Html.Kendo().DatePicker().Name("EndDate").Value(Model.EndDate).HtmlAttributes(new { style = "width:250px"}).Events(e => e.Change("endChange")))               

        @Html.ValidationMessageFor(model => model.EndDate, "", new { @class = "text-danger" })
    </div>
</div>

<script>
    function startChange() {
        var endPicker = $("#EndDate").data("kendoDatePicker"),
            startDate = this.value();
        debugger;
        if (startDate) {
            startDate = new Date(startDate);
            startDate.setDate(startDate.getDate() + 1);
            endPicker.min(startDate);

            debugger;

            var endDateValue = endPicker.value();

            if (startDate > endDateValue)
            {
                debugger;
                var validator = $("#StartDate").kendoValidator({
                    rules:
                    {
                        datepicker: function (input)
                        {
                            var stDate = $("#StartDate").data("kendoDatePicker").value();
                            var enDate = $("#EndDate").data("kendoDatePicker").value();

                            if (stDate > enDate)
                            {
                                debugger;
                                return false;

                            } else {
                                return true;
                            }
                        }
                    },
                    messages: {
                        datepicker: "Start Date Should be less than End Date!"
                    }
                }).data("kendoValidator");
            }
        }
    }

    function endChange() {
        var startPicker = $("#StartDate").data("kendoDatePicker"),
            endDate = this.value();

        if (endDate) {
            endDate = new Date(endDate);
            endDate.setDate(endDate.getDate() - 1);
            startPicker.max(endDate);

            debugger;
            var startDateValue = startPicker.value();

            if (startDateValue > endDate) {
                debugger;
                var validator = $("#EndDate").kendoValidator({
                    rules: {
                        datepicker: function (input)
                        {
                            var stDate = $("#StartDate").data("kendoDatePicker").value();
                            var enDate = $("#EndDate").data("kendoDatePicker").value();

                            if (stDate > enDate)
                            {
                                debugger;
                                return false;
                            }
                            else
                            {
                                return true;
                            }
                        }
                    },
                    messages: {
                        datepicker: "End Date Should be greater than Start Date!"
                    }
                }).data("kendoValidator");
            }

        }
    }
</script>

在这里我试图添加自定义验证,如果开始日期>结束日期

这适用于初始更改,但在第二次更改之后验证未触发,我如何才能正确触发此验证

4

1 回答 1

1

你可以使用jquery来实现。请为该日期选择器设置 id 或类名并使用以下代码

var inputdate1= $('#inputdateid1').data("kendoDatePicker").value();
var inputdate2= $('#inputdateid2').data("kendoDatePicker").value();
if (new Date(inputdate1) > new Date(inputdate2))
{
      alert(" Your alert message !");
}
于 2019-12-19T15:01:22.803 回答