1

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> 
3
  • You use a GreaterOrEqual operator on two strings and give an error message about an invalid date? Commented Mar 18, 2020 at 6:45
  • @HansKesting the textboxes 1 and 2 are used to get start date and end date from the user, when the image button is clicked, it will displays the calendar for the user to pick the date. Commented Mar 18, 2020 at 6:49
  • you know those texts are supposed to be dates, but how is that validator to know? Solution: set its Type property, but note the remarks - server side only for dates Commented Mar 18, 2020 at 7:20

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

EnableClientScript="True" is not working for me. but i will try the other options you gave. Thanks
I think the custom validator will work, it's a bit more work though

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.