Solved

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

Posted on 2009-07-01
1
550 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
1 Comment
 
LVL 41

Accepted Solution

by:
guru_sami earned 500 total points
ID: 24756078
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

773 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