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

ASP.Net
1. ADO.net Database
2. Ajax
3. Asp Control
4. Collections
5. Components
6. Data Binding
7. Development
8. File Directory
9. HTML Control
10. Language Basics
11. Login Security
12. Mobile Control
13. Network
14. Page
15. Request
16. Response
17. Server
18. Session Cookie
19. Sitemap
20. Theme Style
21. User Control and Master Page
22. Validation by Control
23. Validation by Function
24. WebPart
25. WPF
26. XML
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 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)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.