Datetime validation with custom format. Troubles with '.' separator

19,729

Solution 1

Trouble with a format was not with ASP tags and fields but with unobtrusive validation jquery library. I had to overload unobtrusive JavaScript validation method for date for my date format. More details are here MVC DateTime validation - UK Date format

Solution 2

You're looking at a date component. But remember that time components can include seconds and fractional seconds, with a period inbetween. Since dates and times are frequently combined, I would not be surprised if the period is being treated as a special character.

Update:

I went through the following documentation chain:

DisplayFormatAttribute.ApplyFormatInEditMode Property

DisplayFormatAttribute.DataFormatString Property

Formatting Types

Custom Date and Time Format Strings

I expected to see that the period character has a special meaning in the format string. Instead, the documentation doesn't specifically list the period character. That means it should fall in the categry of "Any other character", and therefore, "The character is copied to the result string unchanged."

However, I don't believe it. Fractional seconds occur in time components like 23:59:59.999, and I suspect there is undocumented special treatment of the period character.

Share:
19,729
Vitalii
Author by

Vitalii

merge keep

Updated on June 15, 2022

Comments

  • Vitalii
    Vitalii almost 2 years

    In my web application (asp.new mvc) I try to make validation for date field.

    If I use this

    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime? PeriodBeginFrom { get; set; }
    

    or this

    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd-MM-yyyy}")]
    public DateTime? PeriodBeginFrom { get; set; }
    

    it works. But if I try to separate using '.' separator

    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd.MM.yyyy}")]
    public DateTime? PeriodBeginFrom { get; set; }
    

    I have an issue. jQuery validation says that I have to enter date if I try '01.01.2001'.

    Here is view

    <div class="ElementDiv">
       @Html.LabelFor(m => m.Filter.PeriodBeginFrom, new { @class = "Labels" })
       @Html.EditorFor(m => m.Filter.PeriodBeginFrom, new { @class = "TextBoxes" })
       @Html.ValidationMessageFor(m => m.Filter.PeriodBeginFrom, null, new { @class = "Errors" })
    </div>
    

    Why? With all other separators it works. What a problem is with '.' separatior?

    Update 1: It seems that validation does not work properly. If I have this attribute at my model

    [DataType(DataType.Date)]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime? PeriodBeginFrom { get; set; }
    

    and we pass DateTime object to model, it will be displayed at textbox with '.' like '12.12.2012' and not like I was waiting '12/12/2012'. Hope you'll give me some ideas.