Click here to Skip to main content
15,880,364 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

HTML
.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

C#
[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; }


What I have tried:

tried using datetype and also some regular expressions
Posted
Updated 18-May-16 3:59am

1 solution

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:
C#
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


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.
 
Share this answer
 
Comments
user 3008 19-May-16 6:49am    
Thanks for your reply. I also tried using dateformat. Another part of this issue is that , I am getting this error message only in the chrome but it works fine in IE and firefox

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900