Just wandering around how I can check the date, this is not a future date in .net C #.
Example:
I applied validation there for the necessary field validation. But for some reason, I have no idea how to apply the check to check the start date, to make sure that this is not a future date (the date is not greater than the current date)?
<tr> <td align="right">Start Date:</td> <td><asp:TextBox runat="server" ID="activeDate" size="8"/>(YYYY-MM-DD) <asp:RequiredFieldValidator ID="reqvactiveDate" runat="server" ControlToValidate="activeDate" Display="Dynamic" EnableClientScript="true" ErrorMessage="required" /> </td> </tr>
than I wrote the following code to try out date validation. Check the date does not work for me: (
<tr> <td align="right">Start Date:</td> <td><asp:TextBox runat="server" ID="activeDate" size="8"/>(YYYY-MM-DD) <asp:RequiredFieldValidator ID="reqvactiveDate" runat="server" ControlToValidate="activeDate" Display="Dynamic" EnableClientScript="true" ErrorMessage="required" /> <asp:CustomValidator runat="server" ID="valDateRange" ControlToValidate="activeDate" onservervalidate="valDateRange_ServerValidate" ErrorMessage="enter valid date" /> </td> </tr> " runat = "server" <tr> <td align="right">Start Date:</td> <td><asp:TextBox runat="server" ID="activeDate" size="8"/>(YYYY-MM-DD) <asp:RequiredFieldValidator ID="reqvactiveDate" runat="server" ControlToValidate="activeDate" Display="Dynamic" EnableClientScript="true" ErrorMessage="required" /> <asp:CustomValidator runat="server" ID="valDateRange" ControlToValidate="activeDate" onservervalidate="valDateRange_ServerValidate" ErrorMessage="enter valid date" /> </td> </tr>
code behind:
protected void valDateRange_ServerValidate(object source, ServerValidateEventArgs args) { DateTime minDate = DateTime.Parse("1000/12/28"); DateTime maxDate = DateTime.Parse("2011/05/26"); DateTime dt; args.IsValid = (DateTime.TryParse(args.Value, out dt) && dt <= maxDate && dt >= minDate); } args) protected void valDateRange_ServerValidate(object source, ServerValidateEventArgs args) { DateTime minDate = DateTime.Parse("1000/12/28"); DateTime maxDate = DateTime.Parse("2011/05/26"); DateTime dt; args.IsValid = (DateTime.TryParse(args.Value, out dt) && dt <= maxDate && dt >= minDate); }
source share