...

View Full Version : Is this a valid string?



Phalanxer
06-26-2012, 07:53 AM
if (document.forms["form"]["lol"].value != "1" || document.forms["form"]["lol"].value != "2") {;}
alert ("Illegal value entered.");
return false;
}


When I enter '1' '2' and '3', I am being returned false.
This should only be happening with '3'.

What have I done wrong?

Philip M
06-26-2012, 08:02 AM
When I enter '1' '2' and '3', I am being returned false.
This should only be happening with '3'.

What have I done wrong?

a) unmatched braces {;}

b) Your test should be && (and) not || (or). 2 is not equal to 1 and 1 is not equal to 2. So always returns false;


We are in unprecedented territory, and we have been here before. Presenter, BBC Radio 2

Phalanxer
06-26-2012, 08:08 AM
Sorry I'm real pathetic. That braces was a typo here, its not actually a type in my code:





if (document.forms["form"]["lol"].value != "1" || document.forms["form"]["lol"].value != "2") {
alert ("Illegal value entered.");
return false;
}

Phalanxer
06-26-2012, 08:10 AM
It worked. I don't know why. To me, I interpret it as if its not 1 or 2, where as now with the && I interpret it as if its not 1 and 2. How can it be 1 and 2? I can only input one entry.

Philip M
06-26-2012, 08:33 AM
It worked. I don't know why. To me, I interpret it as if its not 1 or 2, where as now with the && I interpret it as if its not 1 and 2. How can it be 1 and 2? I can only input one entry.

If it's not 1 AND it's not 2.. return false if BOTH conditions are satisfied.

|| (or) says return false if EITHER condition is satisfied.

Phalanxer
06-26-2012, 08:47 AM
Thanks for that! Its good to be reminded never to think your flawless. LuLz

felgall
06-26-2012, 10:16 AM
When you add a NOT to the conditions you need to switch AND for OR and OR for AND

The opposite of 'A or B' is 'not A and not B'.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum