MVC Validation to accept DateTime in ddMMyyyy format
Asked Answered
K

3

7

I just want to accept Date in ddMMyyyy format while submiting. But its gives an error like

The field FromDate must be a date.

enter image description here

But, When I put date in MMddyyyy it accepts pkkttrfg roperly.
My Model is

public class CompareModel 
{
    [Required]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime FromDate { get; set; }

    [Required]
    [DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
    public DateTime TODate { get; set; }

}

My View Part is

<div class="form-group">
        @Html.LabelFor(model => model.FromDate, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.FromDate)
            @Html.ValidationMessageFor(model => model.FromDate)
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(model => model.TODate, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.TODate)
            @Html.ValidationMessageFor(model => model.TODate)
        </div>
    </div>


    <div class="form-group">
        <div class="col-md-offset-2 col-md-10">
            <input type="submit" value="Create" class="btn btn-default" />
        </div>
    </div>
Keyes answered 13/2, 2015 at 11:57 Comment(1)
You can look at using jquery globalize or add a method to the $.validator as per this answer - note that solution is for the jquery ui datepicker but could be adjusted for a standard textbox (e.g. split the value into its components, construct a new javascript Date() object and test its valid)Lasley
J
11

The problem is behind the scenes it uses javascript to validate this and you need to overwrite this.

jQuery(function ($) {
    $.validator.addMethod('date',
    function (value, element) {
        if (this.optional(element)) {
            return true;
        }

        var ok = true;
        try {
            $.datepicker.parseDate('dd/mm/yy', value);
        }
        catch (err) {
            ok = false;
        }
        return ok;
    });
});

Which was taken from this answer

Johnsonjohnsonese answered 13/2, 2015 at 13:24 Comment(2)
Where do I put this? Thanks. I will upvote once I get it working.Enochenol
This requires jquery-ui to work, but is the best answer as it actually fixes the problemChromium
I
4

Just try adding

 <system.web>
    <globalization culture="en-GB"/>
</System.web>

Its work for me with same issue

Interim answered 27/4, 2015 at 4:45 Comment(0)
T
1

same kind of question i answer before DatePicker format issue

Try to set the culture in web config that match with date format.

<globalization uiCulture="en" culture="en-AU" />

See this link ASP.NET Date time support for different cultures

http://maheshde.blogspot.com.au/2011/09/aspnet-date-time-support-for-different.html

Tankersley answered 27/4, 2015 at 4:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.