0

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.

  • Why in the world are you using that regex? Its a `DateTime` property. and you should delete it. Server side validation of a `DateTime` property is based on parsing the value based on the server culture. It looks like you want the date in `dd/MM/yyyy` format, so unless you have set the culture (e.g. in the web.config` file, it will use invariant culture which expect dates in `MM/dd/yyyy` or `yyyy-MM-dd` (ISO) format –  Feb 03 '17 at 22:02
  • I was forced to use this regex validation. As far as I know the above regex validates the date in format mm/dd/yyyy. I have no culture settings in web.config or anywhere else. Can I do that in above Regex attribute or any other attribute of the same property? Please advice – Raghu Adiraju Feb 03 '17 at 22:13
  • What is the purpose of the regex? And what is the value you are entering that is resulting in `ModelState` being invalid? (and as a sidenote, `[DataType(DataType.Date)]` is only respected when using `EditorFor()` but your using `TextBox()`) –  Feb 03 '17 at 22:23
  • The value that I entered is "05/04/2015". The purpose is to validate the input (text box) to send only valid date in the format (mm/dd/yyyy). I was using this regex validation to force the user to enter valid date before the control is posted back. We will get our page wire-frames latter with date picker etc., where we implement jquery form validation latter. – Raghu Adiraju Feb 03 '17 at 22:33
  • But you do not need that regex to do that at all (the property is `DateTime` so if its not a valid date, then validation will fail anyway). And a value of `05/04/2015` passes that regex anyway so if `ModelState` is invalid, its not due to that. Inspect `ModelState` to determine what the actual error message is. –  Feb 03 '17 at 22:37
  • I agree. If i don't use regex, "05/04/875" will also be accepted as valid date. I removed [DataType(DataType.Date)] and tried EditrFor as well with regex validation. No change. ModelState is invalid for this field only. The model state for other three text boxes on the page that takes regular text are valid. Only textbox with Regex marked as invalid. – Raghu Adiraju Feb 03 '17 at 23:08
  • A value of `05/04/2015` passes, but `05/04/875` does not (and its not a valid date anyway so would be invalid for `DateTime` so not sure why you expect it to be valid) - refer [RegExr](http://www.regexr.com/3f7ik) –  Feb 03 '17 at 23:12
  • Thank you so much Stephen, for your time. FYI - "05/04/875" is also a valid date for the property of type DateTime in C#. That is the reason i'm using regex to validate the input in unobtrusive way. – Raghu Adiraju Feb 03 '17 at 23:38
  • So what is your regex for? - are you trying to validate it within a certain range? If so use a `RangeAttribute`. Its not clear what your trying to achieve here. If you had entered `05/04/875` then it would have failed on the client and you would never have been able to submit so what your saying is very confusing. –  Feb 03 '17 at 23:43
  • Apologizes. For e.g., the property doesn't have the regex attribute or any other validation at all and imagine that is bound to a control on page via a view model object. "05/04/875" is a valid date when the control is posted back to server. So I'm using the regex to control user to input a date between 1/1/1800 to 12/31/2099. RegEx strictly forces the user to enter only a date between this range and also forces the date format mm/dd/yyyy. I hope you understand now. I'm not sure that RangeAttribute will enforce the date format. – Raghu Adiraju Feb 04 '17 at 00:07
  • Use a `RangeAttribute` - thats what its for and it works with dates - `[Range(typeof(DateTime), "1/1/1800", "12/31/2099", ErrorMessage = "...")]` (generally a `RegularExpressionAttribute` should only be used for for properties which are typeof `string`) –  Feb 04 '17 at 00:12
  • http://stackoverflow.com/questions/21777412/mvc-model-validation-for-date. I tested the above attribute. It gives continuous client side error for any value entered in text box control. – Raghu Adiraju Feb 04 '17 at 01:07
  • My apologizes, I said it worked but had forgotten I have overridden the `$.validator`. I have my own plugin which overrides many methods to suit whatever culture you want (by default its `en-US`). I can't repeat all that here (to long), but I have created [this DotNetFiddle](https://dotnetfiddle.net/Qp2stI) that includes a script you can add to make the `RangeAttribute` work with `DateTime` –  Feb 04 '17 at 04:39
  • I have also now added an answer to the question you linked above. (i'll delete the fiddle in a few days) –  Feb 04 '17 at 04:56

0 Answers0