Forum Moderators: open

Message Too Old, No Replies

Incorrect number comparison

5 is less than 2? Since when?

         

AWildman

12:23 pm on Jun 6, 2006 (gmt 0)

10+ Year Member



I have a vbscript that is checking to see whether someone has chosen too many of an input. In this particular case, it is checkboxes. I attach a limitValue (max. number of answers allowed for that input) to the input name and strip off the value. I get the value just fine. Then, I grab the answers for that input, and see how many there are (my lvrCount). I get that number just fine. So, I go to compare the two numbers. Let's say my lvrCount is 5 and my limitValue is 2. The user has selected too many answers. The script is telling me that lvrCount is less than limitValue. No matter what. I've made sure that they are numeric so that I'm doing a proper comparison. BUT, I've tried subtracting limitValue from lvrCount and I get the right number, in this case 3. What am I doing wrong?

Here's the script

For i = 1 To Request.Form.Count
inputName = request.form.key(i)
inputVal = request.form.Item(i)

Set regx = New RegExp

'find fields in which no more than a set ceiling can be chosen but at least one must be chosen.
regx.Pattern = "_lvr\d$"
regx.IgnoreCase = True
reglvrVal = regx.Test(inputName)
limitValue = Trim(Right(inputName,1))

'if the input is an lvr, add the input name, value, and limitValue to an array
if reglvrVal = true then
if inputVal = "" then
displayName = regx.Replace(inputName, "")
missingElements = missingElements & "<strong>" & displayName & "</strong><br>"
else
dim iva
iva = Split(inputVal,",")
lvrCount = UBound(iva)

missingElements = missingElements & "<br><Br>lvrCount: " & lvrCount & " limitval: " & limitValue & "<br>"
missingElements = missingElements & "<br>limitValue: " & limitValue & " is numeric: " & isnumeric(limitValue) & "<br>"
missingElements = missingElements & "<Br>lvrcount: " & lvrCount & " is numeric: " & isnumeric(lvrCount) & "<br>"
missingElements = missingElements & "<Br>lvrcount (" & lvrCount & ") - limitValue (" & limitValue & ") equals " & (lvrCount - limitValue)
if (lvrCount > limitValue) then
missingElements = missingElements & begLimitErrorMsg & limitValue & displayName & "s</strong><br>"
missingElements = missingElements & "lvrcount: " & lvrCount & " IS GREATER THAN limitValue " & limitValue & "<br><br>"
else
missingElements = missingElements & "<br>lvrcount: " & lvrCount & " IS LESS THAN OR EQUAL TO limitValue " & limitValue & "<br><br>"
end if
end if
end if
Next

Here is the result of the script:

lvrCount: 5 limitval: 2

limitValue: 2 is numeric: True

lvrcount: 5 is numeric: True

lvrcount (5) - limitValue (2) equals 3
lvrcount: 5 IS LESS THAN OR EQUAL TO limitValue 2

At this point, I don't care if I DID do something stupid, I just want my misery to end. Help! Please! Where did I go wrong?

<added>I just tried setting lvrCount to 5 and limitValue to 2, overriding the values from the array length and input name. THEN, the comparison worked. So, is the "isnumeric" lying to me when it is saying that lvrCount and limitValue are both numbers? Even if it was lying, why would I be able to subtract the two variables, but not compare them?</added>

AWildman

12:47 pm on Jun 6, 2006 (gmt 0)

10+ Year Member



Okay, I figured that for some reason, isnumeric was lying. I don't know why I could subtract but couldn't compare. I had tried doing a CInt on limitValue earlier, but the script returned an error. I don't know why. So, right before I compare the numbers, I changed them to CLng. For some reason, that worked.

While my original problem is solved (can't get the correct comparison) I would really like to know why I couldn't compare in the first instance.

mattglet

6:37 pm on Jun 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just throwing this idea out there...

I think the problem was because your variable was being treated as type 'variant' (as all ASP variables truly are) and not 'integer' (numeric). So without explicitly setting the data type of the variable, you got the weird results. In the future, just always explicitly assign the data types of the variables you're trying use/compare.