I just want to accept Date in ddMMyyyy format while submiting. But its gives an error like
The field FromDate must be a date.
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>
$.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 javascriptDate()
object and test its valid) – Lasley