German Wear Discount Shop - Click Here Write for Dotnet-friends and earn for your each submission [Dot]Net-Friends
Skip Navigation Links
Home
Latest
Fast Code
Articles
Tutorials
Online Resources
Forums
Login   | Hi, Guest


How to use CustomValidator for Date comparison in ASP .NET

Written by omerkamal on Jul 03, 2007
compare two dates using CustomValidator

Explanation:

<%@ Page language="c#" %>
<%@ Import Namespace="System.Drawing" %>

  "c#" runat="server">
  protected void ValidateTravelData(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
  {
    args.IsValid = false;
    DateTime departDate, returnDate;
    feedbackLabel.ForeColor = Color.Red;
    try 
    {
        departDate = DateTime.Parse(flightDepartureDateTextBox.Text);
    catch (Exception ex
    {
        feedbackLabel.Text = "Invalid data entry: Departure Date is invalid. " +
                             "Enter a valid date, for example:  01/20/2006";
        return;
    }
    try 
    {
        returnDate = DateTime.Parse(flightReturnDateTextBox.Text);
    catch (Exception ex
    {
        feedbackLabel.Text = "Invalid data entry: Return Date is invalid. " +
                             "Enter a valid date, for example:  01/20/2006";
        return;
    }
    // Verify that the departure date is less than the
    // return date - no same day trips in this system!
    if (departDate >= returnDate)
    {
        feedbackLabel.Text = "Invalid data entry: The Departure Date must be " 
                             "earlier than the Return Date and no same-day " 
                             "returns for this travel package!"
        return;
    }
    // Verify that the departure date is not in the past or today!
    if (departDate < DateTime.Now)
    {
        feedbackLabel.Text = "Invalid data entry:  The Departure Date cannot " +
                             "be in the past or today!"
        return;
    }
    // Everthing is valid - set the IsValid flag...
    args.IsValid = true;
  }
   

  private void bookTheTripButton_Click(object sender, EventArgs e)
  {
    // Has the page been validated for all data entry?
    if (!(Page.IsValid))
    {
      return;
    }
    // We're all set - book the flight!
    DateTime departDate, returnDate;
    departDate = DateTime.Parse(flightDepartureDateTextBox.Text);
    returnDate = DateTime.Parse(flightReturnDateTextBox.Text);
    feedbackLabel.ForeColor = Color.Black;
    feedbackLabel.Text = "Success!  Your trip from Chicago to London " 
                         "will depart on the " + departDate.ToLongDateString() 
                         " and return on the " + returnDate.ToLongDateString();
  }


<html>

  <body>
    

Travel: Chicago to London


    <form id="TravelForm" method="post" runat="server">

    
      "Panel" runat="server" Width="504px" Height="89px" 
      BackColor="Wheat">Departure Date:
      "flightDepartureDateTextBox" runat="server"
      Width="80px" Height="22px"/>Return Date:
      "flightReturnDateTextBox" runat="server"
       Width="80px" Height="22px"/>

      "validateFlightDepartureDate" runat="server"
      ErrorMessage="Please enter a valid Departure Date.  "
      ControlToValidate="flightDepartureDateTextBox" />
      "validateFlightReturnDate" runat="server"
      ErrorMessage="Please enter a valid Return Date."
      ControlToValidate="flightReturnDateTextBox" />
      "validateFlightDates" runat="server"
      ControlToValidate="flightDepartureDateTextBox"
      OnServerValidate="ValidateTravelData" />
      


    


    "bookTheTripButton" runat="server" Text="Book This Trip"
              OnClick="bookTheTripButton_Click" />
     


     


     "feedbackLabel" runat="server" BackColor="Wheat" Font-Bold="True"
              Text="Select your options, then click the 'Book This Trip' button!" />
     


    form>
  body>
html>
Visitors/Readers Comments
(for questions please use The Forum)



2msoft
Good thanks

01/09/2007 11:16:19 UTC

Remo
Good one !

01/11/2007 02:11:04 UTC

Rajesh
Nice

12/11/2007 02:43:07 UTC

Hiregoudar

this code helped me lot..   Its good one  Thanks for the code

28/11/2007 08:43:27 UTC

amol kinge
very nice but i want also able to select date within 7 days from todays date........if any body knows please let me know..........

29/01/2008 02:52:31 UTC




Add your Comments

Name:  
Message:
Note: For faster response please use Forums >> for your questions instead of the comments area! (Admin)