...

View Full Version : Incorrect return from > in if statement based on number of digits in values



jasendorf
12-22-2005, 04:51 AM
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.

gph
12-22-2005, 05:28 AM
I don't see where the string values are converted to numbers

alert(typeof maxDL)


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

jasendorf
12-22-2005, 05:41 AM
Thanks... I'll try that right now and get back with you.

jasendorf
12-22-2005, 05:49 AM
That did it:

parseInt(maxDL) > parseInt(maxActive)

now returns the proper results. Thanks for the clue.

John

Kor
12-22-2005, 11:28 AM
Better use Number(string) instead of parseInt(string). If there will be an empty input, parseInt() will return NaN, while Number() will return 0....

jasendorf
12-22-2005, 12:04 PM
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!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum