I have some error correcting code, and I wanted to test to make sure that a variable assigned by “Request.Form[“checkbox”]” is either the value given when it is checked (let’s say “ticked”) or whatever the value would be if it was unchecked.
I declare the variable as an empty string before I try to assign it a value with Request.Form
I have tried the following:
if(IsGangMember != "ticked" || IsGangMember != "")
{
errorMessage = "The \"Search For Street Gang Member\" field contains an invalid value.";
}
And this:
if(IsGangMember != "ticked" || IsGangMember != null)
{
errorMessage = "The \"Search For Street Gang Member\" field contains an invalid value.";
}
And this:
if(IsGangMember != "ticked" || IsGangMember.IsEmpty())
{
errorMessage = "The \"Search For Street Gang Member\" field contains an invalid value.";
}
I have also tried printing the value to the screen (if box is unchecked) to see it and it is, of course, empty, so I don’t see how the value could be anything other than an empty string or null.
Any help is appreciated, Thanks!
UPDATE:
Okay, all the solutions given make perfect sense but when unchecked, the error message still gets set.
This is literally all I am doing with it:
var IsGangMember = "";
Then:
IsGangMember = Request.Form["sIsGangMember"];
(sIsGangMember is the name of the input checkbox field)
Then:
if(IsGangMember != "ticked" || !String.IsNullOrEmpty(IsGangMember))
{
errorMessage = "The \"Search For Street Gang Member\" field contains an invalid value.";
}
When left unchecked, the errorMessage still gets assigned. 🙁 Why?
Use
String.IsNullOrEmptyThink should be the other way round