logo
  • Jobs
  • About Me
  • Contact
  • Home
« JetBrains .Net Profiler
What will Google come up with next? »

Date Validation in ASP.NET

Posted January 28th, 2005 by Matt Berther

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. ;)

This entry was posted on Friday, January 28th, 2005 at 1:40 pm and is filed under Uncategorized. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Trevor Adams
February 1st, 2005

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.

yogesh kumar jaiswal
May 10th, 2005

validation for any types

Alex Zubriy
May 20th, 2005

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

Onkar Bakshi
September 1st, 2005

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.

Jayavel Chakravarthy
February 1st, 2006

thank you for above code.. it is so use ful

nil
February 2nd, 2006

hey
thks………..it works……… :)

Camilo Jimenez
February 26th, 2006

Thanx a lot man!!!

Chaitanya
March 31st, 2006

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

pritz
April 6th, 2006

send me date validation.

aurora
April 15th, 2006

thanks a lot.u really save my time!!!

Afis
May 12th, 2006

Thanks a lot dude…..that worked fine……..

Joe
June 1st, 2006

Thanks, hope it will work for my project too ;)

Justin
August 8th, 2006

Great, only thing is it lets blank text go through and won’t catch it.

Lenton
August 17th, 2006

Then pair it with a RequiredFieldValidator

mohd waseem uddin
August 22nd, 2006

how to validate date in asp.net.

Chris
October 27th, 2006

Could not locate your source for the ‘Date Validation in ASP.NET’. Can you send or post the URL to the source? - Thanks, Chris

mady
March 12th, 2007

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

TsiON
December 5th, 2007

thanks for the info

Prawin
January 23rd, 2008

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.

sqlgrl
February 12th, 2008

Very helpful. Thanks!

John
March 31st, 2008

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

Matt Berther
March 31st, 2008

@john: You could probably just use a regular expression validation.

dotnet
April 24th, 2008

hello this code seem to be good but we required one format like (mm/dd/yyyy) in that code format is not specified

larissa
July 10th, 2008

Thanks!! helped a lot!

tania
July 21st, 2008

i have used above validation but its nt workin fine …. can u gimme more
thnx

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
-->

Social
  • mattberther on twitter
Syndication
Archives
  • June 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • February 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005
  • May 2005
  • April 2005
  • March 2005
  • February 2005
  • January 2005
  • December 2004
  • November 2004
  • October 2004
  • September 2004
  • August 2004
  • July 2004
  • June 2004
  • May 2004
  • April 2004
  • March 2004
  • February 2004
  • January 2004
  • December 2003
  • November 2003
  • October 2003
  • September 2003
  • August 2003
  • July 2003
  • June 2003
  • May 2003
  • April 2003
  • March 2003
Jobs
mattberther.com © 2003 - 2008