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 13 of 13
  1. #1
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    What is wrong with this if statement

    I have a form and it allows someone to enter a user level, rather number 1 or 5, so I made this code to check if its 1 or 5, but it doesnt work, I get the alert everytime no matter what number I enter. I am new to javascript, but I just cant see anything wrong with this.

    PHP Code:
    var ul=document.forms["edit"]["userlevel"].value;
    if (
    ul != || ul != 5)
      {
      
    alert("Please enter a user level 1 or 5");
      return 
    false;
      } 
    Thanks
    Ben

  • #2
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    What is wrong with this if statement
    Code:
    return false;
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #3
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    well, it doesn't make sense to have a return value in the if statement in the code you provided... if it were part of a function it would return out of the function, I believe (based on your post) that you expect it to return out of the if statement.
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #4
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I tried it without the return false;
    However I get the alert message, but when I click ok, it continues and submits the form with a number that isnt 1 or 5

  • #5
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by 1bellb View Post
    PHP Code:
    if (ul != || ul != 5
    That statement is testing for the number being not equal to one OR not equal to five.

    So if ul = 1 then it fails the ul != 1 part but passes the {b}ul != 5{/b] part and so runs the alert. Any other value in ul will pass the ul != 1 part and so will run the alert.

    The only situation where the alert would not run is if ul is equal to 1 and 5 both at the same time (which is of course impossible).

    To test for what you actually want you need not equal to 1 and not equal to 5 which is written:

    PHP Code:
    if (ul != && ul != 5
    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.

  • #6
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ow and yes it is part of a function

  • #7
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    yeah, I read your post wrong... you actually *do* intend to return false (again, I knew something was missing from your snippet, I just couldn't figure out what, I got it now)

    PHP Code:
    var ul=document.forms["edit"]["userlevel"].value
    if (
    ul == || ul == 5
      { 
      
    alert("Please enter a user level 1 or 5"); 
      return 
    false
      } 
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #8
    New to the CF scene
    Join Date
    Feb 2012
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by felgall View Post
    That statement is testing for the number being not equal to one OR not equal to five.

    So if ul = 1 then it fails the ul != 1 part but passes the {b}ul != 5{/b] part and so runs the alert. Any other value in ul will pass the ul != 1 part and so will run the alert.

    The only situation where the alert would not run is if ul is equal to 1 and 5 both at the same time (which is of course impossible).

    To test for what you actually want you need not equal to 1 and not equal to 5 which is written:

    PHP Code:
    if (ul != && ul != 5
    That works, thanks, I knew it would be something simple LOL
    Thanks

  • #9
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,035
    Thanks
    203
    Thanked 2,539 Times in 2,517 Posts
    As blaze says, you can only have a return statement within a function.

    The other big error is if (ul != 1 || ul !=5) {

    which should be

    if (ul != 1 && ul !=5) {

    Any number must be not equal to 1 or not equal to 5. (1 != 5, 5 !=1)
    Code:
    <script type = "text/javascript">
    
    function abc() {
    var ul= Number(document.forms["edit"]["userlevel"].value);
    if (ul != 1 && ul !=5)   { 
    alert ("Please enter a user level 1 or 5"); 
    return false;
    }  
    alert ("The user level is valid");
    }
    
    abc();
    
    </script>
    Last edited by Philip M; 02-15-2012 at 08:34 PM.

    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.

  • #10
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    I got it wrong again?? this statement really throws me off
    rather number 1 or 5, so I made this code to check if its 1 or 5
    yikes!!
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #11
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Um, no, Blaze, you still have it wrong.

    With your code, if the user *DOES* enter either 1 or 5 he gets the message.

    Do as Felgall suggested.

    Or add a "not" to your code:
    Code:
        var ul=document.forms["edit"]["userlevel"].value; 
        if ( ! (ul == 1 || ul == 5) )
        { 
             alert("Please enter a user level 1 or 5"); 
             return false; 
        }
    DeMorgan's laws give us
    NOT ( A OR B )
    is the same as
    (NOT A) AND (NOT B)

    ************

    EDIT: Man am I slowing down. Senility really is setting in.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #12
    Regular Coder
    Join Date
    Apr 2005
    Location
    Texas
    Posts
    448
    Thanks
    24
    Thanked 63 Times in 63 Posts
    I know Old Pedant, I jsut can't raed twoday
    Allwisend bin ich nicht, doch viel ist mir bewursst
    -Goethe

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,602
    Thanks
    78
    Thanked 4,387 Times in 4,352 Posts
    Yeah, sorry. I hadn't hit REFRESH before responding and that post had been sitting in my browser for several minutes while I answered a phone call.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Posting Permissions

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