I asked this question previously while trying to use datepicker to validate a date in asp.net mvc 4.5.1. Now, I have eliminated datepicker and am just trying to validate a date to mm/dd/yyyy format using data annotations in the model and regex to force the format. Here is my code:
Model:
[DisplayName("Start Date")]
[DataType(DataType.Date, ErrorMessage = "Date not valid.")]
[DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
[RegularExpression(@"^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$", ErrorMessage = "Date must be in format mm/dd/yyyy")]
public DateTime? DT_Project_Start { get; set; }
View:
<div class="form-group">
@Html.LabelFor(model => model.DT_Project_Start, new { @class = "control-label col-md-2 CreateEditFieldNamesSpan" })
<div class="col-md-10">
@Html.TextBoxFor(model => model.DT_Project_Start)
@Html.ValidationMessageFor(model => model.DT_Project_Start)
</div>
</div>
Doesn't work...validation works perfectly while IN the form, but when I click "submit" it says invalid date NO MATTER what, even if the date is valid and in the right format. Does anyone know what I am missing?