Solved

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

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

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

840 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