show error message if user enter dates beyond the date range

Hi there!,

            I have a textbox where the user enters date. The textbox can only accept dates betweeen 3/17/08 and 06/16/08. If the user enters date before 3/17/08 and after 06/16/08, then I would have to show an error message. This textbox is not a required field and can be left blank. But if the date the user enters is not within the specified dates, then I will have to show them an error. Thanks in advance.
siddhuoopsAsked:
Who is Participating?
 
Bob LearnedConnect With a Mentor Commented:
Is skipping an option?

         if (this.textBox1.Text.Trim().Length > 0)
        {
            DateTime dt1 = new DateTime(2008, 3, 17);
            DateTime dt2 = new DateTime(2008, 6, 18);

            DateTime dt = DateTime.Parse(this.textBox1.Text);

            if (dt < dt1 || dt > dt2)
                MessageBox.Show("Invalid date entered");
         }

Bob
0
 
SweatCoderCommented:
Consider using .NET's calendar control and built-in date range validation. That will save you a lot of work.
0
 
siddhuoopsAuthor Commented:
how can I use the built in date range validation from a calendar control? Could you give me an example?
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
Bob LearnedCommented:
Example:

            DateTime dt1 = new DateTime(2008, 3, 17);
            DateTime dt2 = new DateTime(2008, 6, 18);

            DateTime dt = DateTime.Parse(this.textBox1.Text);

            if (dt < dt1 || dt > dt2)
                MessageBox.Show("Invalid date entered");

Bob
0
 
siddhuoopsAuthor Commented:
Hey Bob, this code works fine but what if the user does not enter anything in the box. Since, the textbox is not a required field.
0
 
siddhuoopsAuthor Commented:
thanks Bob!!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.