When I am using this:
case "vic":
if ((bPostcodeSubstring == 3) || (bPostcodeSubstring == 8)){
return true;
}
else{
errMsg += "Enter A Valid Postcode.";
result = false;
}
break;
It is working fine. But when I am using this:
case "vic":
if ((bPostcodeSubstring != 3) || (bPostcodeSubstring != 8)){
errMsg += "Enter A Valid Postcode.";
result = false;
}
break;
It is not working at all. What's the problem?
When I am using this:
case "vic":
if ((bPostcodeSubstring == 3) || (bPostcodeSubstring == 8)){
return true;
}
else{
errMsg += "Enter A Valid Postcode.";
result = false;
}
break;
It is working fine. But when I am using this:
case "vic":
if ((bPostcodeSubstring != 3) || (bPostcodeSubstring != 8)){
errMsg += "Enter A Valid Postcode.";
result = false;
}
break;
It is not working at all. What's the problem?
Share Improve this question edited Oct 21, 2015 at 4:42 Abhas Tandon 1,88916 silver badges26 bronze badges asked Apr 21, 2014 at 9:35 HelmBurgerHelmBurger 1,3086 gold badges18 silver badges42 bronze badges4 Answers
Reset to default 6!((bPostcodeSubstring == 3) || (bPostcodeSubstring == 8))
is NOT same as
(bPostcodeSubstring != 3) || (bPostcodeSubstring != 8)
it should be
(bPostcodeSubstring != 3) && (bPostcodeSubstring != 8)
demorgan's law
If you want both 3 and 8 unacceptable for this variable, then you have to use &&
.
if ((bPostcodeSubstring != 3) && (bPostcodeSubstring != 8)){
errMsg += "Enter A Valid Postcode.";
result = false;
}
The or
is resolving always to true
, consider if bPostcodeSubstring is 3 then first condition fails but second gives true. If you want to exclude 3 and 8 then use not outside of both or condition
if (!(bPostcodeSubstring == 3 || bPostcodeSubstring == 8)){
OR use and &&
instead of ||
If you want to exclude 3 and 8 then use not outside of both or condition
if ((bPostcodeSubstring != 3) && (bPostcodeSubstring != 8)){
You should use &&
insead of ||
in your second if
.
Indeed, the opposite of :
(bPostcodeSubstring == 3) || (bPostcodeSubstring == 8)
Is :
(bPostcodeSubstring != 3) && (bPostcodeSubstring != 8)