验证日期格式 [英] Validate date format

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

问题描述

我有文本框和附近的日历图标,用户可以在其中输入日期或从日历中选择日期。如果用户键入错误的日期(如40/05/2016或23/14/2016),则会显示错误消息。我的观点有



I have got textbox and also the calendar icon nearby where the user can wither type in the date or select the date from calendar. In case if case if the user types in the wrong date like "40/05/2016" or "23/14/2016" then an error message to be displayed. My view has

.Columns(c =>
        {
            c.Bound(o => o.ReviewerVacationId).Hidden();
            c.Bound(o => o.VacationStartUtc);
            c.Bound(o => o.VacationEndUtc);
            c.Bound(o => o.VacationStartUtcEffective).ReadOnly();
            c.Bound(o => o.VacationEndUtcEffective).ReadOnly();
            c.Bound(o => o.CreatedByWindowsUsername).ReadOnly();
            c.Command(commands =>
            {
                commands.Edit();
                commands.Delete();
            }).Width(100);
        })





我的型号有





and my model has

[Display(ResourceType = typeof(Strings), Name = "SupplierVacationStartUtc")]
       //[DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
       //[DataType(DataType.DateTime, ErrorMessage = "Invalid date")]
       [RegularExpression("^(0?[1-9]|[12][0-9]|3[01])-(0?[1-9]|1[0-2])-(\"d\"d\"d\"d)  (00|[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9])$", ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "SupplierVacationEndUtc")]
       public DateTime VacationStartUtc { get; set; }

       [Display(ResourceType = typeof(Strings), Name = "SupplierVacationEndUtc")]
       [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
       [DataType(DataType.DateTime, ErrorMessage = "Invalid date")]
       public DateTime VacationEndUtc { get; set; }





我尝试过:



尝试使用datetype和一些正则表达式



What I have tried:

tried using datetype and also some regular expressions

推荐答案

,ErrorMessageResourceType = typeof (字符串),ErrorMessageResourceName = SupplierVacationEndUtc)]
public DateTime VacationStartUtc { get ; set ;}

[显示(ResourceType = typeof (字符串),Name = SupplierVacationEndUtc)]
[DisplayFormat(ApplyFormatInEditMode = true ,DataFormatString = {0:dd / MM / yyyy})]
[DataType(DataType.DateTime,ErrorMess) age = 无效日期)]
public DateTime VacationEndUtc { get ; set ; }
", ErrorMessageResourceType = typeof(Strings), ErrorMessageResourceName = "SupplierVacationEndUtc")] public DateTime VacationStartUtc { get; set; } [Display(ResourceType = typeof(Strings), Name = "SupplierVacationEndUtc")] [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")] [DataType(DataType.DateTime, ErrorMessage = "Invalid date")] public DateTime VacationEndUtc { get; set; }





我尝试过:



尝试使用datetype和一些正则表达式



What I have tried:

tried using datetype and also some regular expressions


使用正则表达式验证日期并不容易。变量太多了,比如几个月有30天,有些有31天,2月通常有28天,但每4年左右有29天。



您是否考虑过使用 DateTime.TryParse Method [ ^ ]相反?



示例:

It is not that easy to validate dates with a regular expression. There are too many variables, such as some months have 30 days, others have 31, and February normally has 28 days but every 4 years or so it has 29 days.

Have you considered using the DateTime.TryParse Method[^] instead?

Example:
string s1 = "40/05/2016";
string s2 = "23/14/2016";
string s3 = "29/02/2016";

DateTime date;
bool b1 = DateTime.TryParse(s1, out date); // -> false
bool b2 = DateTime.TryParse(s2, out date); // -> false
bool b3 = DateTime.TryParse(s3, out date); // -> true, because 2016 is a leap year





因为我不精通MVC,所以我不能给你准确的语法,但你应该能够从上面的代码片段中找出它。



Because I am not proficient with MVC, I cannot give you the exact syntax, but you should be able to figure it out from the code snippet above.


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

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