ASP MVC 5 Client Validation for Range of Datetimes

后端 未结 5 1655
旧巷少年郎
旧巷少年郎 2020-12-06 12:44

I want to check an Datetime field in a form. The field is valid between 01/10/2008 and 01/12/2008. Here is how I defined the viewmodel property:

    [Require         


        
5条回答
  •  难免孤独
    2020-12-06 13:21

    Though portions of this have been posted before and downvoted as not working. I can confirm this does work as long as you have both the range and the format string specified.

    [Required]
    [DataType(DataType.Date)]
    [DisplayFormat(DataFormatString = "{0:mm-dd-yyyy}", ApplyFormatInEditMode = true)]
    [Range(typeof(DateTime), "1/1/1900", "12/31/2018",
        ErrorMessage = "Value for {0} must be between {1} and {2}")]
    [Display(Name = "Date of Birth")]
    public DateTime DateOfBirth { get; set; }
    

提交回复
热议问题