Annotation Used:
[RegularExpression("^((0[13578]|1[02])[-/.]31[-/.](18|19|20)[0-9]{2})|((01|0[3-9]|1[0-2])[-/.](29|30)[-/.](18|19|20)[0-9]{2})|((0[1-9]|1[0-2])[-/.](0[1-9]|1[0-9]|2[0-8])[-/.](18|19|20)[0-9]{2})|((02)[-/.]29[-/.](((18|19|20)(04|08|[2468][048]|[13579][26]))|2000))$", ErrorMessage = "Invalid Date")]
[DataType(DataType.Date)]
public DateTime? StartDate { get; set; }
Html:
<div class="form-group col-xs-6">
<label for="StartDate" class="control-label">Start Date:</label>
@Html.TextBox("StartDate", Model.StartDate, new { @class = "form-control" })
@Html.ValidationMessageFor(m => m.StartDate, "", new { @class = "text-danger" })
</div>
When form posted back, the view model object has the value entered but ModelState.IsValid
is false
. ModelState.ErrorCount
is 1
and ModelState["StartDate"]
has SubKey={StartDate}, Key="StartDate", ValidationState=Invalid
. Not sure why I'm still having ValidationState
as Invalid when page has no client side error when entered valid date and could successfully posted back.