tags:

views:

49

answers:

3

I would like to easily validate dates in this format and only this format. Any otehr format should be considered invalid.

+1  A: 

I don't know if regex is possible in VB.NET, but then it'll be relatively easy:

/[0-9]{2}-[a-zA-Z]{3}-[0-9]{4}/

This might be a bit language specific when it comes down to escaping characters and matching certain groups.

wvd
While regex is surely a valid approach your sample would allow `99-fOO-0000` which isn't a valid date.
Filburt
This is true -- but I shouldn't have answered this question since I also kind of knew there were API functions who would be much better at this.
wvd
Truth is I'm a total looser with regex beyond your sample so I hoped you would take it a step further and give me something to go "Ah!".
Filburt
+3  A: 

You use DateTime.ParseExact or DateTime.TryParseExact. You pass through the exact format string.

In your case the format string would be d-MMM-yyyy (see here) and can be used as follows:

string dateString = "31-JUL-2010";
string format = "d-MMM-yyyy";
DateTime result = DateTime.ParseExact(dateString, format, CultureInfo.InvariantCulture);
Oded
+2  A: 
Dim DateToTest As String = "01-Apr-1964"
Dim ResultDate As Date

Date.TryParseExact(DateToTest, "dd-MMM-yyyy", Globalization.CultureInfo.InvariantCulture, Globalization.DateTimeStyles.AllowWhiteSpaces, ResultDate)
Velika