tags:

views:

227

answers:

3

I'm using DateTime.ParseExact to parse a string from input. What is the simplest way to make sure the date complies to rules like max days in a month or no 0. month?

+11  A: 

You could do additional validation using...

DateTime.TryParse();

http://msdn.microsoft.com/en-us/library/9h21f14e.aspx

Hope this helps.

BoltBait
+2  A: 

As BoltBait said, or DateTime.TryParseExact() if you know the exact format of the string.

http://msdn.microsoft.com/en-us/library/system.datetime.tryparseexact.aspx

M4N
+2  A: 

The answers for doing the DateTime.TryParse() are great if you are doing this check in your code behind, but if you are capturing this data on the UI, then I would highly reccomend validing the input as well before the postback to the code behind occurs.

<strong>Date:</strong>
<asp:textbox ID="txtEnterDate" runat="server"></asp:textbox>
<asp:CompareValidator ID="cvEnterDate" runat="server" ControlToValidate="txtEnterDate"
    ErrorMessage="Must Be Valid Date" Operator="DataTypeCheck" 
    SetFocusOnError="True" Type="Date"></asp:CompareValidator>
RSolberg