Click here to Skip to main content
15,891,682 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to validate the DateOfBirth using regular Expression.
Which full fill the below condition together
Date of Birth should be in DD-MM-YYYY format (like 12-12-2014 etc) and date of birth should not be future date
Any one please help me
Posted
Comments
What have you tried? Where is the issue?
Ganesh_verma 19-Dec-14 11:18am    
I need regular expression
Ganesh_verma 20-Dec-14 9:09am    
I am using WSDL validation that accept only regular expression fro validation that's the region I need regex expression.
DamithSL 19-Dec-14 11:05am    
what is the application type you working on?
Ganesh_verma 19-Dec-14 11:18am    
C#, Web service Application in C#

Don't try to use a Regex: they are Text processors, and to validate a date correctly is a real pain: think about leap years for example.

Instead, use DateTime.TryParseExact - if it works it even gives you the data you need to check the date is in range:
C#
DateTime dtBirth;
if (DateTime.TryParseExact(input, "dd-MM-yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dtBirth))
    {
    if (dtBirth < DateTime.Now.Date)
        {
        ...
        }
    }
 
Share this answer
 
Comments
Ganesh_verma 19-Dec-14 11:38am    
I need only regular expression not this
OriginalGriff 19-Dec-14 11:45am    
Don't even think about trying to validate dates with a regex: it's not a good idea at all. Think about it: if "31-01-2001" is valid, how do you write a regex that says "31-02-2001" isn't?
C#
string input ="12-12-2014";
DateTime dt;
if (DateTime.TryParseExact( input, "dd-MM-yyyy",CultureInfo.InvariantCulture,DateTimeStyles.None, out dt)
        && dt < DateTime.Now)
{
    //validation success
}
 
Share this answer
 
Comments
Ganesh_verma 19-Dec-14 11:39am    
I need regular expression not this Please provide the expression
DamithSL 19-Dec-14 11:46am    
^([1-9]|0[1-9]|1[0-2])[- / .]([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])[- / .](1[9][0-9][0-9]|2[0][0-9][0-9])$
Ganesh_verma 20-Dec-14 8:53am    
Thanks for your time but it is not workinng
Date is date and nothing else, no matter of displayed format!

You don't need any regular expression. You need to convert text into DateTime[^]. Please, see Solution1 or Solution2.
 
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