User Error Checking

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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

if (answer1 = = "" | | answer1 != "yes" | | answer1 != "no"

give error message for answer

else if (answer2 = = "" | | answer2 != "yes" | | answer2 != "no"

give error message for answer

els

do my stuf


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

Travi
(e-mail address removed)
 
you've got your logic wrong - (answer1 != "yes" | | answer1
!= "no") is always true (show me a string that equals both "yes" and "no"...
 
You may use switch instead of ||. This could be a bit easier ;-)

switch(answer.ToUpper())
{
case "":
case "YES":
case "NO":
// go ahead
break;

default:
// problem
break;
}

You should also check if your string could be "null" (depending of your
situation)

José

Travis said:
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.
 
Back
Top