0

How can i set the date format for an asp-validation-for? The input is dd/mm/yy and i have set a min and max date but the validation message displays as yyyy-mm-dd. enter image description here

Model

public class ValidateNullDate
{
    [Required]
    [DataType(DataType.Date, ErrorMessage = "Date is required")]
    [DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime? Date { get; set; }
}
<form asp-controller="Date" asp-action="ValidateNullDate" method="post">

    <input type="date" asp-for="Date" min="2019-01-01" max="2020-12-31" />
    <span asp-validation-for="Date" class="text-danger" ></span>

    <button class="btn btn-primary btn-block text-white btn-user" type="submit">Submit</button>
</form>

I am not using a plugin to handle the date picker. I am using jquery validation etc.

mj1313
  • 7,930
  • 2
  • 12
  • 32
Barnsley
  • 117
  • 2
  • 17

1 Answers1

0

I think it's difficult,the client side validation is that the jquery.validate.js plugin used by jquery.validate.unobtrusive.js validates dates based on MM/dd/yyyy format and your entering dates based on a dd/MM/yyyy format.

You can see this thread.

I think the easiest way to achieve this error message is not to use jquery validation,because you write min="2019-01-01" max="2020-12-31",when we submit,The client will automatically validate.

You can see the test: enter image description here

Yinqiu
  • 6,609
  • 1
  • 6
  • 14
  • Thanks for the feedback. I will give this a go and provide feedback. – Barnsley Dec 07 '20 at 10:22
  • i implemented the above from the link provided and it fired ok but produced the incorrect result. The validation message came back as 'Please enter a valid date', looking into the issue it appears chrome is supplying the value 'YYYY-MM-MM' instead of YYYY/MM/DD and so the 'format' does not match the regex. How can I change this? – Barnsley Dec 10 '20 at 09:44
  • You can add your detailed code that is the error message. – Yinqiu Dec 10 '20 at 09:51
  • But how do i change the error message itself? That is the issue for me. That post doesn't make that clear. I want the message to say 'Please enter a date greater than or equal to 01/01/2019' and not 2019-01-01 – Barnsley Dec 10 '20 at 13:04