Solved

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

Posted on 2009-07-01
1
544 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now