ASP.NET - Compare Validator problem - Asked By mostafa hamdy on 18-Dec-11 06:45 AM

Hello all
I have web page contains 2 tetx boxes and 2 dateextendar controls  and one compare validator control  the validator compares the first date with the next date and if the first greater than or equal the next one the validator alert the user , but there's some problem in some cases the validator doesn't work if I entered for example the first date "12/12/2011" and the next date "13/12/2011"  or "14/12/2011" the code I did is as the following:

<

asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:CalendarExtender ID="CalendarExtender1" runat="server"

TargetControlID="TextBox1">

</asp:CalendarExtender>


<
asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>

<asp:CalendarExtender ID="CalendarExtender1" runat="server"

TargetControlID="TextBox2">

</asp:CalendarExtender>


<asp:CompareValidator ID="CompareValidator1" runat="server"

ControlToCompare="TextBox1" ControlToValidate="TextBox2" ErrorMessage="Error"

ForeColor="#FF3300" Operator="GreaterThan"></asp:CompareValidator>
<script>

function




ValidatePage() {



if (Page_ClientValidate()) {



return true;

}




else {

hideValidatorCallout();




return false;

}

}




function hideValidatorCallout() {

AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout.hide();

setTimeout(showValidatorCallout, 3000);

}




function showValidatorCallout() {

AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout = $find(


'<% =CompareValidator1.ClientID %>');

AjaxControlToolkit.ValidatorCalloutBehavior._currentCallout.show(


true);

}




</script>
please if any body get what I mean and can help me please send me or tell me about some url may help me in solving this problem
regards
Mostafa

dipa ahuja replied to mostafa hamdy on 18-Dec-11 06:48 AM
You have to add the Type propety of the Compare Validator

for ex:

Date :<asp:TextBox ID="TextBox1" runat="server" ReadOnly="true"/>
 
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<cc1:CalendarExtender ID="TextBox1_CalendarExtender" runat="server" Enabled="True"
TargetControlID="TextBox1"/>
 
<asp:CompareValidator ID="CompareValidator1"
runat="server" ControlToValidate="TextBox1"
ErrorMessage="Enter Valid Date:" Operator="DataTypeCheck" Type="Date"
Text="Enter Valid Date:" />
 
 

Neha Garg replied to mostafa hamdy on 18-Dec-11 07:22 AM
Hello

Here is the .aspx code to compare the two dates.....

<
html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>:: Date Validator ::</title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <table border="0" cellpadding="2" cellspacing="1">
      <tr>
        <td><asp:Label ID="lblMsg" runat="server" Text="Enter Start Date (dd/mm/yyyy)"></asp:Label></td>
        <td><asp:TextBox ID="txtDate1" runat="server"></asp:TextBox> <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtDate1" ErrorMessage="*" Operator="DataTypeCheck" Type="Date"></asp:CompareValidator></td>
      </tr>
      <tr>
        <td><asp:Label ID="lblMsg2" runat="server" Text="Enter End Date (dd/mm/yyyy)"></asp:Label></td
        <td><asp:TextBox ID="txtDate2" runat="server"></asp:TextBox> <asp:CompareValidator ID="CompareValidator2" runat="server" ControlToValidate="txtDate2" ErrorMessage="*" Operator="DataTypeCheck" Type="Date"></asp:CompareValidator></td>
      </tr>
      <tr>
        <td colspan="2">
          <asp:CompareValidator ID="cmpStartDate" runat="server"
            ControlToValidate="txtDate1"
            ErrorMessage="Start date cannot be less than today's date"
            Operator="GreaterThanEqual" Type="Date"></asp:CompareValidator>
          <asp:CompareValidator ID="cmpEndDate" runat="server"
            ErrorMessage="End date cannot be less than start date"
            ControlToCompare="txtDate1" ControlToValidate="txtDate2"
            Operator="GreaterThanEqual" Type="Date"></asp:CompareValidator>
        </td>
      </tr>
      <tr>
        <td colspan="2"><asp:Button ID="btnValidate" runat="server"  Text="Validate"/></td>
      </tr>
    </table>
  </div>
  </form>
</body>
</html>
Web Star replied to mostafa hamdy on 18-Dec-11 08:20 AM
Yes if you want to compaire date with another date then you should use CompareValidator control to validate those date and if you are using ajax toolkit to display messages then you can use ValidatorCalloutExtender control to display message only you need to specify correct id of ControlToCompare and ControlToValidate
Here i tested your condition and assume you TextBox1 is start date and TextBox2 is end date and Textbox2 should be greater than Textbox1 date, it works perfectly
Note that no need any javascript function for that only make sure the format of date should be same and Validation group will be correct

Change your code as follows
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:CalendarExtender ID="CalendarExtender1" runat="server"

Format




="dd/MM/yyyy"

TargetControlID="TextBox1">

</asp:CalendarExtender>


<
asp:TextBox ID="TextBox2" runat="server" ></asp:TextBox>

<asp:CalendarExtender ID="CalendarExtender1" runat="server"

Format




="dd/MM/yyyy"

TargetControlID="TextBox2">

</asp:CalendarExtender>



<

asp:CompareValidator ID="CVHolidayDates" runat="server" ControlToCompare="TextBox1"

ControlToValidate="TextBox2" Display="None" ValidationGroup="valAddHoliday"

ErrorMessage="Please end date should be greater than start date."

Operator="GreaterThanEqual" Type="Date"></asp:CompareValidator>







<asp:ValidatorCalloutExtender ID="vceCVHolidayDates" runat="server" Width="260" TargetControlID

="CVHolidayDates">







</asp:ValidatorCalloutExtender>

Same ValidationGroup should  be use for your submit button that will work perfectly.

kalpana aparnathi replied to mostafa hamdy on 18-Dec-11 12:10 PM

Hi There,

 Your compare validator something like this:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox1"

ErrorMessage="CompareValidator" Operator="operatortype" Type="Date"></asp:CompareValidator>


And Try to put this on page_load event
protected void Page_Load(object sender, EventArgs e)

{

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

}

Anoop S replied to mostafa hamdy on 18-Dec-11 11:48 PM
use this, its working code 

<
asp:ScriptManager id="ScriptManager1" runat="server"></asp:ScriptManager>
   
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
   
<cc1:CalendarExtender ID="CalendarExtender1" runat="server" TargetControlID="TextBox1">
   
</cc1:CalendarExtender>
   
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <cc1:CalendarExtender
        ID="CalendarExtender2" runat="server" TargetControlID="TextBox2">
   
</cc1:CalendarExtender>
   
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="TextBox1"
        ControlToValidate="TextBox2" ErrorMessage="Textbox2 is greater then Textbox1"
        Operator="GreaterThan" Type="Date"></asp:CompareValidator>
Jitendra Faye replied to mostafa hamdy on 18-Dec-11 11:54 PM
Try this example-

Note = "TextBoxBookingDeadline" value comparing with the "TextBoxSeminarDate" value as follow.

 

<



asp:CompareValidator ID="CompareValidatorBookingDeadline" runat="server"


ControlToCompare

="TextBoxSeminarDate"


ControlToValidate

="TextBoxBookingDeadline" Display="Dynamic"


ErrorMessage

="Please check the seminar date and select appropriate date for booking deadline"


Operator

="LessThanEqual"


Type

="Date"


ValueToCompare

="<%= TextBoxSeminarDate.Text.ToShortString() %>">*</asp:CompareValidator>


 

The important thing is "ValueToCompare" property of the compare validator. Here I have assined it to the value of the TextboxSeminarDate and then compered it with the booking deadline date.


Hope this helps,