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>
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
Just try adding
<system.web>
<globalization culture="en-GB"/>
</System.web>
Its work for me with same issue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With