Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I compare two date fields in asp.net?

Posted on 2009-07-01
1
Medium Priority
?
556 Views
Last Modified: 2013-11-26
I have written the attached code for two textbox controls (OpenDateInsertTextBox, ClosingDateInsertTextBox) attached to ajax calendar extenders to compare each other so that the ClosingDate is never before the OpenDate. Unfortunately the compare only works when the OpenDates day is less than the ClosingDates day, i.e. OpenDate: 15/07/2009 ClosingDate: 13/08/2009 flags an error?
<td class="item">
                                                                <asp:TextBox ID="OpenDateInsertTextBox" runat="server" CssClass="calendarTxtBox"></asp:TextBox>
                                                                
                                                                <asp:RequiredFieldValidator ID="OpenDateValidator" runat="server" ControlToValidate="OpenDateInsertTextBox" ErrorMessage="*" SetFocusOnError="true" Display="Dynamic" ValidationGroup="insert"></asp:RequiredFieldValidator>
                                                                <asp:RegularExpressionValidator ID="OpenDateRegularValidator" runat="server" ErrorMessage="<br><small>Invalid date.</small>" ValidationExpression="^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-./])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$" Display="Dynamic" ControlToValidate="OpenDateInsertTextBox" ValidationGroup="insert"></asp:RegularExpressionValidator>
                                                                <ajaxToolKit:CalendarExtender ID="OpenDateCalendar" runat="server" TargetControlID="OpenDateInsertTextBox" Format="dd/MM/yyyy" CssClass="MyCalendar" PopupButtonID="OpenDateCalendarImage">
                                                                </ajaxToolKit:CalendarExtender>
                                                            </td>
                                                            <td class="item">
                                                                <br />
                                                                <asp:TextBox ID="ClosingDateInsertTextBox" runat="server" CssClass="calendarTxtBox"></asp:TextBox>
                                                                
                                                                <asp:RequiredFieldValidator ID="ClosingDateValidator" runat="server" ControlToValidate="ClosingDateInsertTextBox" ErrorMessage="*" SetFocusOnError="true" Display="Dynamic" ValidationGroup="insert"></asp:RequiredFieldValidator>
                                                                <asp:RegularExpressionValidator ID="ClosingDateRegularValidator" runat="server" ErrorMessage="<br><small>Invalid date.</small>" ValidationExpression="^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-./])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$" Display="Dynamic" ControlToValidate="ClosingDateInsertTextBox" ValidationGroup="insert"></asp:RegularExpressionValidator>
                                                                <asp:CompareValidator 
                                                                    ID="DateValidator" 
                                                                    runat="server" 
                                                                    ControlToCompare="OpenDateInsertTextBox" 
                                                                    ControlToValidate="ClosingDateInsertTextBox" 
                                                                    ErrorMessage="<br><small>Invalid date.</small>" 
                                                                    Operator="GreaterThan" 
                                                                    ValidationGroup="insert">
                                                                </asp:CompareValidator>
                                                                <ajaxToolKit:CalendarExtender ID="ClosingDateCalendar" runat="server" TargetControlID="ClosingDateInsertTextBox" Format="dd/MM/yyyy" CssClass="MyCalendar" PopupButtonID="ClosingDateCalendarImage">
                                                                </ajaxToolKit:CalendarExtender>
                                                            </td>

Open in new window

0
Comment
Question by:Shepwedd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 41

Accepted Solution

by:
guru_sami earned 1500 total points
ID: 24756078
0

Featured Post

Stack Overflow Podcast - Developer Story

Welcome to the Stack Overflow podcast recorded Thursday July 20 at Stack Overflow Headquearters in NYC. Your hosts today are podcast regulars Jay Hanlon, David Fullerton, and Ilana Yitzhaki, plus the quite irregular Matt Sherman (Stack Overflow Engineering Manager extraordinaire)

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question