验证日期在MVC MM / DD / YYYY格式 [英] Validate Date in MM/dd/YYYY format in mvc

查看:301
本文介绍了验证日期在MVC MM / DD / YYYY格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经声明在MVC info文件的属性如

I have declared a property in MVC info file like

  [Required(ErrorMessage = "End Date has not being entered")]
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
        [RegularExpression(@"^(0[1-9]|1[012])[/](0[1-9]|[12][0-9]|3[01])[/]\d{4}$", ErrorMessage = "End Date should be in MM/dd/yyyy format")]
        public DateTime? ExpirationDate { get; set; }

但是,当我进入了像2013年5月13日正确格式的日期。它剧照显示errormessage的是

But when I am entered a date in correct format like 5/13/2013. It stills show the errormessage that

End Date should be in MM/dd/yyyy format

什么code我失踪或有任何其他错误与上述

What code I am missing or there is any other error with the above.

推荐答案

您无法验证与常规的前pression日期,使用DateTime.TryParseExact将字符串转换成一个DateTime对象。正则表达式将永远怀念细微之处,如闰年,等等。

You can't validate dates with regular expression, use DateTime.TryParseExact to convert the string into a DateTime object. Regex will always miss subtleties such as leap years, etc.

这篇关于验证日期在MVC MM / DD / YYYY格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆