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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Incorrect return from > in if statement based on number of digits in values

    I found a bug in an application which isn't even mine and I can't for the life of me figure out why I can't fix it.... I've been testing in IE6 and Fx and the below results are the same.

    Here's the scoop:

    Two HTML entries:
    <input type="text" id="max_active" name="max_active" value="10" size="4" >
    <input type="text" id="max_dl" name="max_dl" value="10" size="4" >

    Two js variables from those entries on submit:
    var maxDL = document.getElementById("max_dl").value;
    var maxActive = document.getElementById("max_active").value;

    IF statement in the js (the original has more qualifiers... but I've at least whittled it down to this particular portion):
    if(maxDL > maxActive) {
    strAlert+= "|" + maxDL + "| |" + maxActive + "|\nMax downloads cannot be higher than Max active\n"
    }

    I added the extra echoes so that I could see the numbers that the js was working with.

    Here's the problem... the alert comes up when:
    maxDL = 8 and maxActive = 10
    maxDL = 90 and maxActive = 120

    It acts as expected when there are the same number of digits in each number. Hence, it works with: maxDL = 8 and maxActive = 9, and when maxDL = 110 and maxActive = 120.

    Another thing I've noticed is that it seems to only read maxActive up to the number of digits in maxDL. Hence, maxDL = 13 and maxActive = 120... it returns true. BUT, maxDL = 11 and maxActive = 120 and it returns false.

    One more thing. It works properly if:
    maxDL = 08 and maxActive = 10
    maxDL = 090 and maxActive = 120



    Do I need some sort of integer declaration or something? I'm just baffled.

  • #2
    gph
    gph is offline
    Regular Coder
    Join Date
    Apr 2005
    Posts
    161
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't see where the string values are converted to numbers

    alert(typeof maxDL)


    Use parseFloat(maxDL), parseInt(maxDL) or Number(maxDL)

  • #3
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks... I'll try that right now and get back with you.

  • #4
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That did it:

    parseInt(maxDL) > parseInt(maxActive)

    now returns the proper results. Thanks for the clue.

    John

  • #5
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Better use Number(string) instead of parseInt(string). If there will be an empty input, parseInt() will return NaN, while Number() will return 0....
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #6
    New to the CF scene
    Join Date
    Dec 2005
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the tip. The original coder actually checks the variables against NaN earlier in the function so that might actually take care of it.

    And, Bucharest is a beautiful city... best of luck with the EU!


  •  

    Posting Permissions

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