check for valid date when textbox is visible

I have asp.net page with checkbox and textbox, if checkbox is checked then textbox is made visible and I then need to check for a valid date.  However if checkbox is not checked then I hide textbox and do not need to check for a valid date.  How can I do this?  This is my code.......

if (checkbox.Checked)
            {
                customer.DateRaised = DateTime.Parse(txtDate.Text);  //check for valid date here
            }
            else
            {
                customer.DateRaised = DateTime.Now;
            }
mugseyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Pratima PharandeCommented:
best way you can use any date control along with textbox
0
Pratima PharandeCommented:
0
mugseyAuthor Commented:
Thanks but I need to use microsoft controls within visual studio
0
Starting with Angular 5

Learn the essential features and functions of the popular JavaScript framework for building mobile, desktop and web applications.

karstiemanCommented:
I can't quite figure out what exactly you want but your code should look like this

if checkbox.checked then
      'show textbox and check for date
       textbox.visible = true
       check for date here
else
     ' just hide textbox
      textbox.visible = false
end if
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DeathraceCommented:
hey,
checkbox should be enabled with Autopostback property,,,see below..for the definition

<asp:CheckBox ID="CheckBox1" runat="server"
            oncheckedchanged="CheckBox1_CheckedChanged" Checked="false" AutoPostBack="true" />

In the Page load event
PageLoad()
{
  txtDate.Visible =false; //make your textbox false
}

In the CheckBox1_checkedChanged()
{
 if (CheckBox1.Checked)
            TextBox1.Visible = true;          
    else
            TextBox1.Visible= false;
}


In the TextBox1_TextChanged event()
{
     customer.DateRaised = DateTime.Parse(TextBox1.Text);  
}
0
Pratima PharandeCommented:
You can try like this

  If CheckBox1.Checked = True Then
            TextBox1.Visible = True
        Else
            TextBox1.Visible = False
            TextBox1.Text = DateTime.Now
        End If

Add regular expression validator with date validation regular expression
when checkbox not selected just set valid date as now
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.