santosh
santosh

Reputation: 71

CompareValidator not working well - ASP.net

I have used compare validator to check whether the selected date is valid or it. The issue here is it only fires up when the submit button is clicked, is it possible to check when the user selects the date.

<tr id="trow" runat="server"> 
            <td class="auto-style3">Need Duration</td>  
            <td class="auto-style2">  
            <asp:TextBox ID="TextBox1" runat="server" ReadOnly = "true"></asp:TextBox>
             <asp:ImageButton ID="imgJoin" runat="server" ImageUrl="Images/calender.png"/>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" 
            ControlToValidate="TextBox1" ErrorMessage="*" ForeColor="Red" 
            SetFocusOnError="True"></asp:RequiredFieldValidator></td>


              <td> 
             <asp:TextBox ID="TextBox2" runat="server" ReadOnly = "true"></asp:TextBox>
              <asp:ImageButton ID="imgHide" runat="server" ImageUrl="Images/calender.png"/>
             <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" 
            ControlToValidate="TextBox2" ErrorMessage="*" ForeColor="Red" 
            SetFocusOnError="True"></asp:RequiredFieldValidator>

             <asp:CompareValidator ID="CompareValidator1" runat="server" Operator="GreaterThanEqual"
              ControlToValidate="TextBox2" ControlToCompare="TextBox1" 
               ErrorMessage='Invalid Date' 
                 ForeColor="Red"></asp:CompareValidator>
             </td>
            </tr>  

Upvotes: 1

Views: 1037

Answers (1)

Stefan
Stefan

Reputation: 17648

It has been a while, but I think you need to enable client side validation scripts by adding:

EnableClientScript="True"

Example

<asp:CompareValidator ID="CompareValidator1" EnableClientScript="True" runat="server" 
          Operator="GreaterThanEqual"
          ControlToValidate="TextBox2" ControlToCompare="TextBox1" 
          ErrorMessage='Invalid Date' 
          ForeColor="Red"></asp:CompareValidator>

It's documented at msdn.


Aditionally, I do know that custom validators often lack a correct implementation of the javascript. I am not sure how the CompareValidatorbehaves in that sense.

You might need to create a inherited class, to implement the scripts fully. Before going there, try do research a bit.

For example, here is a solution with a custom validator

Upvotes: 1

Related Questions