PIZZADOMU.INFO

Turn 2 0 in dates validating comparevalidator with keep her

Gary,

We have a business requirement to stick to mm-dd-yyyy format. And I have specified that in the CalendarExtender object.

Do I need to specify the format somewhere else too?

I am validating the format by using regex, verifying valid date by using CompareValidator, and then doing the range check.

The first CompareValidator correctly throws an InvalidDate error. The range check continues the evaluation, even though there is an invalid date, and evaluates to false throwing the error message - from date > than To Date.

I have attached my latest code here.

Any help will be helpful as this is driving me nuts.

Moving the validation code back to server side means, I won't be able to use the ajax calloutExtender as it does not support server-side validation.

<asp:TextBox ID="txtFromDate" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="rfvFromDate" runat="server" ControlToValidate="txtFromDate" ErrorMessage="*" Display="Static" SetFocusOnError="true" /> <span class="labelSmall"> To Date </span> <asp:TextBox ID="txtToDate" runat="server"></asp:TextBox> <asp:RequiredFieldValidator ID="rfvToDate" runat="server" ControlToValidate="txtToDate" ErrorMessage="*" Display="Static" SetFocusOnError="true" /> <!-- Calendar objects --> <ajaxToolkit:CalendarExtender ID="calFromDate" runat="server" TargetControlID="txtFromDate" Format="MM-dd-yyyy" > </ajaxToolkit:CalendarExtender> <ajaxToolkit:CalendarExtender ID="calToDate" runat="server" TargetControlID="txtToDate" Format="MM-dd-yyyy"> </ajaxToolkit:CalendarExtender> <!-- Calendar - mm-dd-yyyy format validation --> <asp:RegularExpressionValidator ID="regFromDate" Enabled="true" runat="server" ErrorMessage="Please enter a date in mm-dd-yyyy format." ControlToValidate="txtFromDate" Display="None" ValidationExpression="([1-9]|0[1-9]|1[012])[-]([1-9]|0[1-9]|[12][0-9]|3[01])[-](19|20)\d\d"></asp:RegularExpressionValidator> <ajaxToolkit:ValidatorCalloutExtender ID="cmpInvalidFromDate_vldCalloutExtender" runat="server" Enabled="true" TargetControlID="regFromDate"> </ajaxToolkit:ValidatorCalloutExtender> <asp:RegularExpressionValidator ID="regToDate" Enabled="true" runat="server" ErrorMessage="Please enter a date in mm-dd-yyyy format." ControlToValidate="txtToDate" Display="None" ValidationExpression="([1-9]|0[1-9]|1[012])[-]([1-9]|0[1-9]|[12][0-9]|3[01])[-](19|20)\d\d"></asp:RegularExpressionValidator> <ajaxToolkit:ValidatorCalloutExtender ID="cmpInvalidToDate_vldCalloutExtender" runat="server" Enabled="true" TargetControlID="regToDate"> </ajaxToolkit:ValidatorCalloutExtender> <!-- Calendar - Check if its a valid Date Value --> <asp:CompareValidator ID="cmpValidatorDate1" runat="server" ControlToValidate="txtFromDate" Operator=DataTypeCheck Type="Date" Display="None" ErrorMessage="Invalid Date" Enabled="true" SetFocusOnError="true"> </asp:CompareValidator> <ajaxToolkit:ValidatorCalloutExtender ID="cmpValidatorDate1_VldCalloutExtender" runat="server" Enabled="true" TargetControlID="cmpValidatorDate1" > </ajaxToolkit:ValidatorCalloutExtender> <!-- Calendar - Check if its a valid Date Value --> <asp:CompareValidator ID="cmpValidatorDate2" runat="server" ControlToValidate="txtToDate" Operator=DataTypeCheck Type="Date" Display="None" ErrorMessage="Invalid Date" Enabled="true" SetFocusOnError="true"> </asp:CompareValidator> <ajaxToolkit:ValidatorCalloutExtender ID="cmpValidatorDate2_vldCalloutExtender" runat="server" Enabled="true" TargetControlID="cmpValidatorDate2"> </ajaxToolkit:ValidatorCalloutExtender> <!-- Calendar - To date cannot be greater than today validation --> <asp:RangeValidator ID="rngValidatorDate" runat="server" ControlToValidate="txtToDate" ErrorMessage="To Date cannot be greater than today" MinimumValue="01-01-0001" Type="Date" Display="None" Enabled="true"> </asp:RangeValidator> <ajaxToolkit:ValidatorCalloutExtender ID="rngValidatorDate_vldCalloutExtender" runat="server" Enabled="true" TargetControlID="rngValidatorDate" > </ajaxToolkit:ValidatorCalloutExtender> <!-- Calendar - From date cannot be greater than To date validation --> <asp:CompareValidator ID="cmpValidatorDate" runat="server" ControlToCompare="txtFromDate" ControlToValidate="txtToDate" Operator="GreaterThanEqual" Type="Date" Display="None" ErrorMessage="From Date cannot be greater than To Date" Enabled="true"> </asp:CompareValidator> <ajaxToolkit:ValidatorCalloutExtender ID="cmpValidatorDate_vldCalloutExtender" runat="server" Enabled="true" TargetControlID="cmpValidatorDate"> </ajaxToolkit:ValidatorCalloutExtender>

https://forums.asp.net/t/1522484.aspx?Issues+with+CompareValidator+for+dates