Compare Validator for date validation

3k views Asked by At

The following is the code I'm using to select a date in asp.net

I'm using a text box to display the date and image to show the calender icon, and Ajax calender extender to select the date in the format dd-mmm-yyyy.

<asp:TextBox ID="txtReqCompDate" runat="server"
    ToolTip="Select Analysis Required Date" CssClass="formtext"
    CausesValidation="True">
</asp:TextBox>
<asp:ImageButton ID="ibReqCompletionDate" runat="server"
    ImageUrl="~/Images/Calendar.png" ImageAlign="AbsBottom" Height="15px" />
<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
    ControlToValidate="txtReqCompDate" Display="Dynamic" Text="Required"
    ErrorMessage="Analysis Required Date" ValidationGroup="valProjForm"
    SetFocusOnError="True" Font-Size="Smaller">
</asp:RequiredFieldValidator>
<asp:CompareValidator runat="server" ID="cmp1"
    ErrorMessage="The date must be greater than todays date"
    ControlToValidate="txtReqCompDate" Type="Date" Operator="GreaterThan" />
<cc1:CalendarExtender ID="CalendarExtender1" runat="server"
    PopupButtonID="ibReqCompletionDate"
    TargetControlID="txtReqCompDate" Format="dd-MMM-yyyy" Enabled="True">
</cc1:CalendarExtender>

Even If I use this line in page load there is no use.

cmp1.ValueToCompare = DateTime.Now.ToShortDateString();

Please suggest me a way to display a message right away when some one selects a date less than today's date using comparevalidator.

I need a similar functionality in several pages of my website. And also in the same page to select a date greater than txtReqCompDate.

Or please suggest me a way to disable the previous dates in ajax calenderextender and show the date ahead of the current date in Ajax extender without affecting my current code functionality as I'm coding in .net 2.0.

My Main issue here, it shows validation, if date is less than today's date and even when the date is greater than current date. Shows error message always whatever validator I use, compare or range

2

There are 2 answers

2
Manu On

Instead of Compare validator, use range validator

 **In html**  

 <asp:RangeValidator ID="RangeValidator1" runat="server">  
    </asp:RangeValidator>  

 **C#**
     RangeValidator1.ControlToValidate = "txtReqCompDate";  
     RangeValidator1.Type = ValidationDataType.Date;  
     RangeValidator1.MinimumValue = DateTime.Now.ToShortDateString();  
     RangeValidator1.MaximumValue = DateTime.Now.AddDays(7).ToShortDateString();  
     RangeValidator1.ErrorMessage = "Select date between today to next 7 days!";  

You can modify it according to your requirement.

1
Syed Ali Taqi On

Set Type to String of your CompareValidator and then in code behind get the date and format it to string as your requirement (which is "dd-MMM-yy") "Case Sensitive". I have tested this code by entering date manually and it worked.

<asp:CompareValidator runat="server" ID="cmp1"
ErrorMessage="The date must be greater than todays date"
ControlToValidate="txtReqCompDate" Type="String" Operator="GreaterThan" />

Code:

string date = DateTime.Now.ToString("dd-MMM-yy");
cmp1.ValueToCompare = date;