Date Validation in ASP.NET
Ever have the need for a user to enter a date into a textbox, but not sure how to validate that they actually did?
<asp:CompareValidator id="dateValidator" runat="server" Type="Date" Operator="DataTypeCheck" ControlToValidate="dateTextbox" ErrorMessage="Please enter a valid date."></asp:CompareValidator>
This beats the heck out of writing my own code to do this, and surely this is better tested as well. ;)
validation for any types
Wow . Thank you so Much !!!!
You Safe my date.
I was already wrinting custom validation or decidet to write client site jscript.
and WAW.
so easy.
Microsoft RULLS.
Thanks
It seems simple as though but what to do if localization is implemented?? just hardcoding of date ‘1/1/2000′ will throw an exception for Swiss or Chinies Date formats.
thank you for above code.. it is so use ful
hey
thks………..it works……… :)
Thanx a lot man!!!
How to compare dates with the current date meaning restrict the user to enter less than todate?
Is their any way to set the Operator=”????” to check for entered date should be less than the current date
send me date validation.
thanks a lot.u really save my time!!!
Thanks a lot dude…..that worked fine……..
Thanks, hope it will work for my project too ;)
Great, only thing is it lets blank text go through and won’t catch it.
Then pair it with a RequiredFieldValidator
how to validate date in asp.net.
Could not locate your source for the ‘Date Validation in ASP.NET’. Can you send or post the URL to the source? - Thanks, Chris
hi its nice example for validation. But when i select a date it is selected with time by Calender control. But this Validation work only for format Date and not DateTime. Can u please suggest smtg.
Thanks
mady
thanks for the info
It seems simple as though but what to do if localization is implemented?? just hardcoding of date ‘1/1/2000′ will throw an exception for Swiss or Chinies Date formats.
Very helpful. Thanks!
How would we restrict them to enter 4 digits year e.g.
01/01/11
not acceptable
instead
01/01/2011
is acceptable
thanks in advance
@john: You could probably just use a regular expression validation.
hello this code seem to be good but we required one format like (mm/dd/yyyy) in that code format is not specified
Thanks!! helped a lot!
i have used above validation but its nt workin fine …. can u gimme more
thnx
Works great for me and my U.S. based SQL database!
thanks
hi dear
please tell me how to validate ” dd/mm/yyyy” date format using comparevalidator…

Hi Matt - I have used a very similar approach on several occasions. If the business rules demand it, you can add a couple different parameters to make sure that date entered is not before a certain date. For example use these two params: ValueToCompare=”01/01/2000″ Operator=”GreaterThan” with all the other values the same will make sure its a valid date that is not before Jan 1, 2000. Of course it would probably be appropriate to adjust the mesasage that is returned to the user.