Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Banned
    Join Date
    Jun 2012
    Posts
    81
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Is this a valid string?

    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.

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,020
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.

  • #3
    Banned
    Join Date
    Jun 2012
    Posts
    81
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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;
    }

  • #4
    Banned
    Join Date
    Jun 2012
    Posts
    81
    Thanks
    7
    Thanked 0 Times in 0 Posts
    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.

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,020
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.

  • #6
    Banned
    Join Date
    Jun 2012
    Posts
    81
    Thanks
    7
    Thanked 0 Times in 0 Posts
    Thanks for that! Its good to be reminded never to think your flawless. LuLz

  • #7
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,641
    Thanks
    0
    Thanked 649 Times in 639 Posts
    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/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •