Validate Datepicker containing date only

Asked

Viewed 252 times

0

I use Asp.Net Core MVC (Razor + Jquery Validate + Unobtrusive). I display in a Datepicker only the date part of a Datetime field through the following code in my Viewmodel:

[Required(ErrorMessage = "* preenchimento obrigatório")]
[DataType(DataType.Date)]
public DateTime Data { get; set; } = DateTime.Now;

However, in my view, when typing the contents of the field with a valid date, I get the error message "Please enter a Valid date."

When removed the date Annotation [Datatype(Datatype.Date)] and displayed the part of time, the validation works correctly, so no problem of location

I have also added the following Annotation date to Viewmodel, but was unsuccessful:

[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]

I’m not sure, but it seems to me the problem is in unobtrusive validation that seems not to behave well with inputs that display only part of the date.

How to solve this problem?

  • Take a look at this answer and get help: https://stackoverflow.com/questions/6505773/custom-date-format-validation-with-jquery-datepicker-cant-get-rid-of-us-date-v

1 answer

0

Add to that, it should solve:

[DataType(DataType.Time, UIHint("Time")]
public DateTime Data { get; set; }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.