Date Compare Validator Control ASP.NET

Compare two input dates to avoid invalid dates. In this example I have created two textboxes and namded as "TextBoxSeminarDate" and "TextBoxBookingDeadline". Booking deadline date must be before date to the Seminar date. Therefore I used Operator as "LesThanEqual".

I have validated "TextBoxBookingDeadline" value comparing with the "TextBoxSeminarDate" value as follow.


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


ControlToValidate="TextBoxBookingDeadline" Display="Dynamic"

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



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,


  • Nice tool, really helpful, thanks for sharing it on your blog!!..

  • Wow!, this was a real quality post. In theory I’d like to write like this too – taking time and real effort to make a good article… but what can I say… I keep putting it off and never seem to achieve anything

  • Great article. That really saved my time :). Thanks alot man!

  • After it validates and it works ok but i cant use calender again to choose different date....Why?

  • Thanks, it helped me.

  • but only date is validating but if month is greater then the compare validator is not working.

  • Please check your date format, this worked well when I was testing long time a go...

  • I am checking for dd-mmm-yyyy(From Date : 16-jun-2011 & To Date : 22-jun-2011) but still error msg & not able to do any action..pls give me solutionn

  • Hi Chan,

    Your questions is not exactly clear... The above code explained you to compare a date with another date, like registration date and deadline. But it is not from date and to date.
    First you should choose "ControlToCompare" (E.g. Seminar Date) and "ValueToCompare" (E.g. Booking deadline) dates and apply the above senario.

  • i want to enter date in date of birth text box and then i need to validate plz tell me properties i need to change

  • Hi Pasha,

    You can use compare validator as validator and then change the CompareValidator properties as mentioned below.

    ControlToValidate: ID of the date of birth text box
    ErrorMessage: Your error message
    Operator: DataTypeCheck
    Type: Date
    ValidationGroup - If you have any

    If you require more detail pls email me

    Hope this helps,

  • Also you can use RegularExpressionValidator and use below link to find correct expresstion and put it into the ValidationExpression property of the RegularExpressionValidator.

  • Yeah !! It's really Cool Post I Really admire this Post because i never find this solution in anyway easily !! Thanks for Sharing

  • Thanks, It Helped Me

  • Sorry its not working.
    Please check the code in .net 4.0 and update the code

    1.Compare validator must required compare to validate which not be givn in yours code.............

    check it and update......

  • it work well in devloper mode.but after deployement it not validate month show in textbox format-dd/MM/yyyy.
    if we input first value 01/04/2012 and second 01/03/2012 then it not validate.pls help

  • its ok but what happen if the textbox to compare have a format like this "dd-MM-yyyy" dont match with textBoxSeminarDate.Text.ToShortString()

  • Thank u so much for this information

  • hey friends i need help regarding the person's age should be above 18 to sign up, is it possible using Compare Validator on Controltovalidate=textbox ?

  • It's working correctly if entry is 20/11/2012 as a txtArrDateInCity and 18/11/2012 or 20/11/2012 txtArrDateInHotel.
    Problems is that when entry is 20/11/2012 as a txtArrDateInCity and 20/12/2012 txtArrDateInHotel.
    This compares only first 2 values i.e. 20 for txtArrDateInCity And txtArrDateInHotel.
    But how to compare these values 11/2012 or 12/2012.

    Abhishek Rajak..

Comments have been disabled for this content.