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"

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,

Published Tuesday, February 1, 2011 10:20 PM by Sahanr
Filed under: ,

Comments

# re: Date Compare Validator Control ASP.NET

Wednesday, February 2, 2011 1:05 AM by SEO services

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

# re: Date Compare Validator Control ASP.NET

Wednesday, February 16, 2011 5:34 AM by 传奇私服

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

# re: Date Compare Validator Control ASP.NET

Thursday, April 21, 2011 2:37 AM by Shahid

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

# re: Date Compare Validator Control ASP.NET

Friday, June 10, 2011 3:56 AM by mwamba

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

# re: Date Compare Validator Control ASP.NET

Monday, June 20, 2011 1:57 AM by Sankalp

Thanks, it helped me.

# re: Date Compare Validator Control ASP.NET

Monday, July 4, 2011 2:12 AM by Meg

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

# re: Date Compare Validator Control ASP.NET

Friday, August 19, 2011 7:59 AM by Sahanr

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

# re: Date Compare Validator Control ASP.NET

Tuesday, August 23, 2011 6:04 AM by Chan

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

# re: Date Compare Validator Control ASP.NET

Monday, August 29, 2011 3:25 PM by Sahanr

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.

# re: Date Compare Validator Control ASP.NET

Tuesday, September 13, 2011 8:49 AM by pasha

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

# re: Date Compare Validator Control ASP.NET

Tuesday, September 13, 2011 3:27 PM by Sahanr

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 sahanr@live.com

Hope this helps,

Sahan

# re: Date Compare Validator Control ASP.NET

Wednesday, September 14, 2011 3:02 PM by Sahanr

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

regexlib.com/Search.aspx

# re: Date Compare Validator Control ASP.NET

Wednesday, October 12, 2011 9:31 AM by DevConn

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

# re: Date Compare Validator Control ASP.NET

Friday, November 18, 2011 2:24 AM by kiran

Thanks, It Helped Me

# re: Date Compare Validator Control ASP.NET

Wednesday, December 28, 2011 8:53 AM by kapil dev

Sorry its not working.

Please check the code in .net 4.0 and update the code

error..

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

check it and update......

# re: Date Compare Validator Control ASP.NET

Thursday, February 2, 2012 1:25 AM by suryakant4it

it work well in devloper mode.but after deployement it not validate month value.date 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

# re: Date Compare Validator Control ASP.NET

Friday, May 11, 2012 1:30 PM by Juan Pablo

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

# re: Date Compare Validator Control ASP.NET

Friday, August 3, 2012 12:47 AM by Suba

Thank u so much for this information

# re: Date Compare Validator Control ASP.NET

Monday, August 6, 2012 2:38 AM by Raj

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 ?

# re: Date Compare Validator Control ASP.NET

Wednesday, November 21, 2012 4:45 AM by Abhishek

<asp:CompareValidator ID="CompareValidator6" runat="server" ErrorMessage="Arrival Date in Hotel should be greater than or equals to the Arrival Date in City.." style="z-index: 180; left: 90px; position: absolute; top: 795px" ControlToCompare="txtArrDateInCity" ControlToValidate="txtArrDateInHotel" Operator="GreaterThanEqual" Width="426px"></asp:CompareValidator><br />

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.

Thanx,

Abhishek Rajak..