Hi, does anyone know how to add date validation? This is what I currently have:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Calculate_reservation_totals
{
    public partial class CalReservationTotals : Form
    {
        public CalReservationTotals()
        {
            InitializeComponent();
        }

        private void CalReservationTotals_Load(object sender, EventArgs e)
        {
            const int pricePerNight = 115;
            txtPrice.Text = pricePerNight.ToString("£.00");
        }

        private void butCalculate_Click(object sender, EventArgs e)
        {
            DateTime arrival;
            DateTime depart;

            if (String.IsNullOrEmpty(txtArrival.Text) || !DateTime.TryParse(txtArrival.Text, out arrival))
            {
                MessageBox.Show("Please enter a date");
                return;
            }
                arrival = arrival.Date;

            if (String.IsNullOrEmpty(txtDepart.Text) || !DateTime.TryParse(txtDepart.Text, out depart))
            {
                MessageBox.Show("Please enter a date");
                return;
            }
                depart = depart.Date;

            if (depart > DateTime.Today)
            {
                MessageBox.Show("The arrival date cannot be before the departure date");
                return;
            }

            if (arrival < depart)
            {
                MessageBox.Show("The departure date cannot be before the departure date");
                return;
            }

            if (arrival == depart)
            {
                MessageBox.Show("The arrival date and the departure date cannot be on the same date");
                return;
            }

            double nights = (arrival.Subtract(depart).TotalDays);
            double price = nights * 115;

            string numberOfNights = nights.ToString();
            txtNights.Text = numberOfNights.ToString();

            string totalPrice = price.ToString("£.00");
            txtTotal.Text = totalPrice.ToString();
            }
        }

        private void butExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Please give me any suggestions for alteration.

Thanks

Recommended Answers

All 4 Replies

What I mean is, I want to have a date format validation so when the users' enter the date in a different format, it throws back a message saying "invalid date format" or something like that... I'm currently having no luck

            String userDate = "10/13/2012";
            try
            {
                Convert.ToDateTime(userDate);
            }
            catch (System.Exception ex)
            {
                // Error message ...
            }

Have a look here.

O, and if your are looking for date time patterns you can find most (if not all) of them here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.