Martin Haščák
Martin Haščák

Reputation: 350

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

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 everything 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!

Upvotes: 1

Views: 4399

Answers (3)

Vijai
Vijai

Reputation: 2509

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

Upvotes: 2

Gedao
Gedao

Reputation: 1058

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

Upvotes: 0

Hadee
Hadee

Reputation: 1402

try this:

DatePickerFor(model => model.ExpirationDate).Format("{0:dd.MM.yyyy}");

Upvotes: 0

Related Questions