(Advertisment)
German Wear Discount Shop - Click Here
[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" %>

  <script language="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();
  }</script>


<html>
<head></head>
  <body>
    <h1>Travel: Chicago to London</h1>
    <form id="TravelForm" method="post" runat="server">

    <!-- Flight Info -->
      <asp:panel id="Panel" runat="server" Width="504px" Height="89px" 
      BackColor="Wheat">Departure Date:
      <asp:TextBox id="flightDepartureDateTextBox" runat="server"
      Width="80px" Height="22px"/>Return Date:
      <asp:TextBox id="flightReturnDateTextBox" runat="server"
       Width="80px" Height="22px"/></br>
      <asp:RequiredFieldValidator id="validateFlightDepartureDate" runat="server"
      ErrorMessage="Please enter a valid Departure Date.  "
      ControlToValidate="flightDepartureDateTextBox" />
      <asp:RequiredFieldValidator id="validateFlightReturnDate" runat="server"
      ErrorMessage="Please enter a valid Return Date."
      ControlToValidate="flightReturnDateTextBox" />
      <asp:CustomValidator id="validateFlightDates" runat="server"
      ControlToValidate="flightDepartureDateTextBox"
      OnServerValidate="ValidateTravelData" />
      </asp:panel>


    <p>
    <asp:Button id="bookTheTripButton" runat="server" Text="Book This Trip"
              OnClick="bookTheTripButton_Click" />
     </p>
     <p>
     <asp:Label id="feedbackLabel" runat="server" BackColor="Wheat" Font-Bold="True"
              Text="Select your options, then click the 'Book This Trip' button!" />
     </p>
    </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

hirpra
gooooooood

14/03/2009 04:03:49 UTC

Sushant
Get it Very Easily........
Thanks................................

22/07/2010 02:22:29 UTC




Add your Comments

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