首页 > 解决方案 > asp.net mvc 代码中的日期验证优先方法

问题描述

我想对我的模型类进行日期验证,我希望验证限制年龄在 18 岁以下的用户给出错误消息,例如您的年龄不符合要求,日期为日历格式,因此任何不规则的选择像 9999 年 2 月 2 日这样的年份也必须通过使用验证来引发错误,任何人都可以通过使用任何类型的正则表达式来帮助我解决这个问题吗?

代码:

   public class AdmissionModel
   {
     [Key]
     [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
     public int Std_id { get; set; }

     [Required]
     [StringLength(50)]
     public string std_name { get; set; }

     [Required]
     [StringLength(50)]
     public string std_father { get; set; }

     [Required]
     [StringLength(50)]
     public string std_mother { get; set; }

     [Required]
     [DataType(DataType.Date)]
     public string DOB { get; set; }      ---This is the date--
   }

标签: c#asp.netasp.net-mvcasp.net-mvc-4model-view-controller

解决方案


你可以像这样验证你的日期

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

推荐阅读