How to valid date format dd.MM.yyyy KENDO MVC

2019-08-09 09:21发布

i have problem with validation this date format in kedno UI: dd.MM.yyyy For my culture (CS-CZ) kendo use this pattern: d. M. yyyy, but mostly used is format dd.MM.yyyy.

I tried everythnik but no success yet :/

My model:

public DateTime? ExpirationDate { get; set; }

My kendo form:

<div class="form-group">
@Html.LabelFor(model => model.ExpirationDate, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
    <div class="checkbox">
        @(Html.Kendo().DatePickerFor(model => model.ExpirationDate)
          .Name("ExpirationDate")
          .Culture("cs-CZ")
          .HtmlAttributes(new { style = "width:150px" })
        )
        @Html.ValidationMessageFor(model => model.ExpirationDate, "", new { @class = "text-danger" })
    </div>
</div>

Thanks!

3条回答
Luminary・发光体
2楼-- · 2019-08-09 09:50

Use a Javascript function to validate:

function IsValidDate(inputDate) {
    if (kendo.parseDate(inputDate) == null) {
        return false;
    }
    else {
        return true;
    }
}

In View use this

@( Html.Kendo().DatePicker()
     .Name("FirstMODate")
     .Format("dd.MM.yyyy")
     .ParseFormats(new string[] { "dd.MM.yyyy" })
     .Culture("cs-CZ")
     .HtmlAttributes(new { style = "width:100px" })
)
查看更多
Explosion°爆炸
3楼-- · 2019-08-09 09:58

In web config, section system.web configure your culture

<globalization uiCulture="cs-CZ" culture="cs-CZ" />

In layout you need to include kendo culture javascript file

<script src="@Url.Content("~/Scripts/kendo/2015.3.930/cultures/kendo.culture.cs-CZ.min.js")"></script>

and add javascript code

$(function () {
    kendo.culture("cs-CZ");
});

Kendo documentation

Or try to add custom rule for your date input:

kendo.ui.validator.rules.mvcdate = function (input) {
        if ($(input.attr('name')) === 'ExpirationDate') {
            return input.val() === "" || kendo.parseDate(input.val(), "dd.MM.yyyy") !== null;
        }
        return true;
    }
查看更多
劫难
4楼-- · 2019-08-09 09:59

try this:

DatePickerFor(model => model.ExpirationDate).Format("{0:dd.MM.yyyy}");
查看更多
登录 后发表回答