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

I want to create a regular Expression for following for date format

21 Apr 2014

I Have tried some regular expressions but did not get any solution.


C#
(((((([1-9])|(0[1-9])|([0-1][0-2])))[\ ]{1}
([A-Z]{1}\b)([a-z]{2})[\ ]{1}
(([1-9])|([1-2][0-9])|(30)))|(2[\/]{1}(([1-9])|([1-2][0-9]))))[\/]{1}\d{4})
Posted
Comments
[no name] 24-Apr-14 4:03am    
i want restrict the use on selection/enter in date textbox
[no name] 24-Apr-14 5:47am    
http://www.aspdotnet-suresh.com/2012/07/regular-expression-to-validate-date-in.html

If you want to validate a date, regular expression can't.
You should use DateTime.TryParseExact Method[^]
See my example:
C#
using System;
using System.Globalization;

public class Program
{
    public static void Main()
    {
        string dateString = "21 Apr 2014"; // <-- Valid
        string format = "dd MMM yyyy";
        DateTime dateTime;
        if (DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture,  DateTimeStyles.None, out dateTime))
        {
            Console.WriteLine("It is a valid date");
        }
        else
        {
            Console.WriteLine("It is NOT a valid date");
        }
    }
}
 
Share this answer
 
v2
Comments
[no name] 24-Apr-14 4:13am    
i have to validate date textbox
Peter Leow 24-Apr-14 4:45am    
No problem, adapt my example code for your textbox.text, do it yourself. btw, you should use some datapicker or calendar control which will always returns a valid date value which you can use it out of the box without any validation.
Try This:

^(((((0[1-9])|(1\d)|(2[0-8]))-((0[1-9])|(1[0-2])))|((31-((0[13578])|(1[02])))|((29|30)-((0[1,3-9])|(1[0-2])))))-((20[0-9][0-9]))|(29-02-20(([02468][048])|([13579][26]))))$
 
Share this answer
 
Comments
phil.o 24-Apr-14 4:51am    
This is not a strict DateTime validation. For example, this does not allow to validate a leap year, or more precisely it will validate a February, 29th on a non-leap year.
Again, REGULAR EXPRESSIONS ARE NOT MEANT TO VALIDATE DATETIMES!
Sanket Saxena 24-Apr-14 4:58am    
right phil your option is better.
[no name] 24-Apr-14 4:52am    
will this format work for 21 Apr 2014 format date
Sanket Saxena 24-Apr-14 4:58am    
try phil solution it will help you.
Drop regex for this case. Use DateTime.TryParseExact()[^] instead. It's specialized to parsing dates and much more readable.
 
Share this answer
 
Comments
[no name] 24-Apr-14 4:13am    
i need to validate date text box
lukeer 24-Apr-14 6:02am    
As others have mentioned, do it. To validate text input, use the method I suggested. It will tell you whether or not the input is a correct date.
But better would be to use a DateTimePicker[^] control instead of a TextBox. It's there for just that reason.
Just my 2 cents, but regular expressions are the worst choice when it comes to validate datetimes. They are not meant for handling datetime values, they are meant to handle strings.

Better use the DateTime.TryParse() method to validate datetimes.
This way:
C#
DateTime value;
if (DateTime.TryParse(yourStringInput, out value)) {
   // Valid DateTime
}
else {
   // Invalid DateTime
}


There are also some other methods that will allow you to handle specific culture format.
More here:
DateTime Structure[^]
Have a particular attention to TryParse() and TryParseExact() overloads.

Hope this helps. Good luck.
 
Share this answer
 
Comments
[no name] 24-Apr-14 4:13am    
i need to validate date textbox
phil.o 24-Apr-14 5:02am    
Tip: yourStringInput could be replaced by yourTextBox.Text.
The idea of replacing your TextBox by a DateTimePicker could be the best solution, though. You should follow Peter Leow's advice.

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