User Error Catching

  • Thread starter Thread starter Travis
  • Start date Start date
T

Travis

I am trying to make sure that a user enters either a yes or no in a text box
so I tried this statement

if (answer1 = = "" | | answer1 != "yes" | | answer1 !=
"no")
{
give error message for answer 1
}
else if (answer2 = = "" | | answer2 != "yes" | | answer2 !=
"no")
{
give error message for answer 2
}
else
{
do my stuff
}

But the OR statements will not process if the user enters no or yes. It
just sees that the answer is not blank and ignores the rest. How do I make
it check to make sure that not only is it not BLANK but that either YES or
NO was answered and nothing else.

Help!

Travis
(e-mail address removed)
 
Hi Travis,

if you just want to test if the user entered "yes" or "no" you need to use
AND not OR
("the answer is not 'yes' AND the answer is not 'no'"). You don't have to
test explicitly that the answer is not blank, since "answer is neither 'yes'
nor 'no'" includes answer beeing blank.

I.e. your code should look like this:

if(answer1 != "yes" && answer1 != "no")
{
error for answer1
}
else if(answer2 != "yes" && answer2 != "no")
{
error for answer1
}
else
{
do your stuff.
}

Have a nice day
Bernhard
 
Back
Top