Using ASP.NET Validation for comparison to today's date

aspnetWhile working on a ski reservation system for one of my clients, Ski Broker.  I ran across the issue of needing to make sure that when a client registered they didn't set their registration into the past. I took a look through Google to see what I could find and found a few different samples of code to solve the issue.

What I finally ended up with is below:

   1:  <asp:TextBox id="txtDateNeeded" runat="server" />
   2:  <asp:comparevalidator id=valDateNeededCompare runat="server"  
   3:      Operator="GreaterThanEqual" type="Date" 
   4:      controltovalidate="txtDateNeeded" 
   5:      errormessage="The date must be greater than or equal to today." />

This places the necessary code on the page but does not allow the validation to take place yet because we do not have a comparison value yet.  In the code behind file we need to place the following code in the Page_Load event:

   1:  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
   2:          valDateNeededCompare.ValueToCompare = DateTime.Now.ToShortDateString()
   3:  End Sub

Once we run the page now we will get the error message that we specified above in our validation control.  If anyone has any suggestions to make this code better please feel free to post it in the comments section below.


Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Posted on 1/9/2008 8:36:04 AM by admin

Permalink | Comments (0) | Post RSSRSS comment feed |

Categories: ASP.NET | Programming | Software | Visual Studio

Tags: , , ,

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Related posts

Comments are closed