Click here to Skip to main content
15,886,740 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
public static bool hasValidCharforDate(string input)
{
Regex r = new Regex("^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\\d\\d$.");

if (r.IsMatch(input))
{
return false;
}

return true;
}

What I have tried:

Please help me

i am trying to validate date and i need in mm/dd/yyyy format so please give me regex code
Posted
Updated 19-Sep-18 1:01am

1 solution

Don't use regex to validate dates, use DateTime.TryParseExact.

DateTime dt;
string dateText = "01/30/2018";
bool valid = DateTime.TryParseExact(dateText, "MM/dd/yyyy", System.Globalization.CultureInfo.CurrentCulture, System.Globalization.DateTimeStyles.None, out dt);
 
Share this answer
 
Comments
Member 12183079 19-Sep-18 7:03am    
no actually i am validating so please give me regex code
F-ES Sitecore 19-Sep-18 7:08am    
You can use the above code when validating. RegEx is for formats but dates are more than a format

01/30/2000 is valid
02/30/2000 is invalid

02/29/2020 is valid
02/29/2021 is invalid
Member 12183079 19-Sep-18 7:26am    
please write code
F-ES Sitecore 19-Sep-18 7:54am    
You can't use regex to validate dates.
Member 12183079 19-Sep-18 7:59am    
i need code above code is not working

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