Click here to Skip to main content
15,915,869 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I am looking for an exact "dd/mm/yyyy" date validator which also checks for valid dates ex: "31/11/2011" should return an error since Novemeber is only 30 days.

I use the following which validates "dd/mm/yyyy"
([1-9]|0[1-9]|[12][0-9]|3[01])[- /.]([1-9]|0[1-9]|1[012])[- /.][0-9]{4}$

but will not check if the date is invalid like: "31/11/2011"
Posted
Updated 23-Mar-11 1:45am
v3

There are regexes out there which will do this, (mahen25 has linked to site giving at least one) but the problem is that they are very complex, and not very maintainable.

You might be better off just using DateTime.TryParseExact[^] and using the result that way.
C#
DateTime date;
String myDate = "30/11/2011";
if (DateTime.TryParseExact(myDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
    {
    Console.WriteLine("OK");
    }
else
    {
    Console.WriteLine("Error");
    }
String myWrongDate = "31/11/2011";
if (DateTime.TryParseExact(myWrongDate, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
    {
    Console.WriteLine("OK");
    }
else
    {
    Console.WriteLine("Error");
    }
 
Share this answer
 
Comments
Vartkes Nadjarian 23-Mar-11 6:51am    
the link that mahen25 gave me does have what I am looking for. It is similar to the one I have which only checks for the dd/mm/yyyy format but does not check for invalid dates in that format,

30/02/2007 or 31/11/1999 etc... and I want the solution to be on the client side
Hi. I found this article for you http://javascript.internet.com/forms/validate-date.html. Vote if it will help you.
 
Share this answer
 

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