I got 2 DateTime field and i use datepicker for user to choose a date. However, since it is a textbox, user can still enter any value and click the submit button. So, I wish to have validation on the DateTime textfield to check whether the value submitted is a DateTime with specific format. I tried this:
[Required(ErrorMessage = "Storage Date is required")]
[DataType(DataType.DateTime, ErrorMessage = "Please input a valid date")]
public DateTime StorageDate { get; set; }
However, I dint get any message even i input “xxxxx” in the textbox and submit (Note: the [Required] validation do work)
My form is like this:
<div class="editor-label">
Storage Date
</div>
<div class="editor-field">
@Html.TextBox("StorageDate", String.Format("{0:ddd, d MMM yyyy}", DateTime.Now), new { id = "storagedate" })
@Html.ValidationMessageFor(model => model.StorageDate)
</div>
So, I need check the Date according to the format also.. Any idea why it is not working???
Any help is really appreciated…Thanks…
You could add a regular expression annotation to the property of your view model:
Here is a link for a REGEX library – you may choose to use one of their expressions for validating date.