Solved

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

Posted on 2009-07-01
1
554 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 500 total points
ID: 24756078
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

617 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