Date validation: start, end date (C#) : By Your Function « Validation by Control « ASP.Net

ASP.Net
1. ADO.net Database
2. Asp Control
3. Collections
4. Components
5. Data Binding
6. Development
7. HTML Control
8. Mobile Control
9. Page
10. Request
11. Response
12. Server
13. Session Cookie
14. User Control and Master Page
15. Validation by Control
16. Validation by Function
17. XML
Microsoft Office Word 2007 Tutorial
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
ASP.Net » Validation by Control » By Your Function 
Date validation: start, end date (C#)

<%@ 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>

           
       
Related examples in the same category
1. Implement custom validation control event (VB.net)
2. Validate control in code behind in C#
3. Validate control manually in C#
4. Validate by a Server Validate function (VB.net)
5. Validating a Percentage Using the CustomValidator Control (VB.net)
w__w_w_.__j_av_a___2s_._c_om___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.