Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-26-2012, 07:53 AM   PM User | #1
Phalanxer
Banned

 
Join Date: Jun 2012
Posts: 81
Thanks: 7
Thanked 0 Times in 0 Posts
Phalanxer can only hope to improve
Is this a valid string?

Quote:
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?

Last edited by Phalanxer; 06-26-2012 at 07:55 AM..
Phalanxer is offline   Reply With Quote
Old 06-26-2012, 08:02 AM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Phalanxer View Post
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
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 06-26-2012, 08:08 AM   PM User | #3
Phalanxer
Banned

 
Join Date: Jun 2012
Posts: 81
Thanks: 7
Thanked 0 Times in 0 Posts
Phalanxer can only hope to improve
Sorry I'm real pathetic. That braces was a typo here, its not actually a type in my code:

Quote:
Quote:
if (document.forms["form"]["lol"].value != "1" || document.forms["form"]["lol"].value != "2") {
alert ("Illegal value entered.");
return false;
}
Phalanxer is offline   Reply With Quote
Old 06-26-2012, 08:10 AM   PM User | #4
Phalanxer
Banned

 
Join Date: Jun 2012
Posts: 81
Thanks: 7
Thanked 0 Times in 0 Posts
Phalanxer can only hope to improve
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.
Phalanxer is offline   Reply With Quote
Old 06-26-2012, 08:33 AM   PM User | #5
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
Quote:
Originally Posted by Phalanxer View Post
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.
__________________

All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Philip M is offline   Reply With Quote
Old 06-26-2012, 08:47 AM   PM User | #6
Phalanxer
Banned

 
Join Date: Jun 2012
Posts: 81
Thanks: 7
Thanked 0 Times in 0 Posts
Phalanxer can only hope to improve
Thanks for that! Its good to be reminded never to think your flawless. LuLz
Phalanxer is offline   Reply With Quote
Old 06-26-2012, 10:16 AM   PM User | #7
felgall
Master Coder

 
felgall's Avatar
 
Join Date: Sep 2005
Location: Sydney, Australia
Posts: 5,465
Thanks: 0
Thanked 499 Times in 491 Posts
felgall is a jewel in the roughfelgall is a jewel in the roughfelgall is a jewel in the rough
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'.
__________________
Stephen
Learn Modern JavaScript - http://javascriptexample.net/
Helping others to solve their computer problem at http://www.felgall.com/
felgall is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:33 PM.


Advertisement
Log in to turn off these ads.