Jeano Ermitaño
Jeano Ermitaño

Reputation: 179

Check if conditionally hidden textboxes have values

I have a list of YES/NO questions and each question has a radio button indicating the answer. When the user selects YES, a panel will be visible and it has textboxes inside it for the additional required input. When the user answers YES, they MUST fill in the textboxes that appear.

Currently I'm hard-coding it this way:

            if (txtQ1Specify.Visible == true)
            {
                if (txtQ1Specify.Text.Length == 0)
                {
                    lblError.Text = "Please answer all questions.";
                }
            }
            if (txtQ2Specify.Visible == true)
            {
                if (txtQ2Specify.Text.Length == 0)
                {
                    lblError.Text = "Please answer all questions.";
                }
            }
            if (txtQ3Specify.Visible == true)
            {
                if (txtQ3Specify.Text.Length == 0)
                {
                    lblError.Text = "Please answer all questions.";
                }
            }
            if (txtQ4SpecifyCompany.Visible == true || txtQ4SpecifyRelative.Visible == true)
            {
                if (txtQ4SpecifyCompany.Text.Length == 0 || txtQ4SpecifyRelative.Text.Length == 0)
                {
                    lblError.Text = "Please answer all questions.";
                }
            }
            if (txtQ5SpecifyCompany.Visible == true || txtQ5SpecifyRelative.Visible == true)
            {
                if (txtQ5SpecifyCompany.Text.Length == 0 || txtQ5SpecifyRelative.Text.Length == 0)
                {
                    lblError.Text = "Please answer all questions.";
                }
            }
            if (txtQ6Specify.Visible == true)
            {
                if (txtQ6Specify.Text.Length == 0)
                {
                    lblError.Text = "Please answer all questions.";
                }
            }
            if (txtQ7Specify.Visible == true)
            {
                if (txtQ7Specify.Text.Length == 0)
                {
                    lblError.Text = "Please answer all questions.";
                }
            }

After this checking I want to execute some code.

The page looks like this:

screenshot

How can I check for textbox inputs based in visibility?

Upvotes: 0

Views: 1104

Answers (3)

Jeano Ermitaño
Jeano Ermitaño

Reputation: 179

I managed to do it using a very long if statement. Here goes nothing:

if ((pnlQ1Yes.Visible == true && txtQ1Specify.Text.Length == 0) ||
            (pnlQ2Yes.Visible == true && txtQ2Specify.Text.Length == 0) ||
            (pnlQ3Yes.Visible == true && txtQ3Specify.Text.Length == 0) ||
            (pnlQ4Yes.Visible == true && (txtQ4SpecifyCompany.Text.Length == 0 || txtQ4SpecifyRelative.Text.Length == 0)) ||
            (pnlQ5Yes.Visible == true && (txtQ5SpecifyCompany.Text.Length == 0 || txtQ5SpecifyRelative.Text.Length == 0)) ||
            (pnlQ6Yes.Visible == true && txtQ6Specify.Text.Length == 0) ||
            (pnlQ7Yes.Visible == true && txtQ7Specify.Text.Length == 0))
{
    lblError.Text = "Please answer all questions.";
}
else   
{
    ...
}

It first checks if the panel is visible, then it checks the textbox inside the panel for values. Then I repeat this kind of checking for the rest of the panels and textboxes.

Upvotes: 0

Midhun Murali
Midhun Murali

Reputation: 2151

This can be easily done on the client side .

  1. First you need to identify which all text box are visible . For this you can use jquery Visible Selector

  2. Now If more than one text box is visible . Show some message to the user and focus and highlight the text box which need to be filled .

$(document).ready(function(){
    var visibleCount =$('input[type="text"]:visible').length;
  if (visibleCount  > 0)
  {
     // Add your logic here
   }
});

Upvotes: 0

You could use LINQ to find out if there are any visible and empty TextBoxes like so:

var query =
    from t in Page.Controls.OfType<TextBox>()
    where t.Visible && t.Text == ""
    select t;

bool hasUnanswered = query.Any();

Upvotes: 1

Related Questions