comparing two numbers

1. Junior Member
Join Date
Dec 1969
Posts
6

comparing two numbers

hi everyone. i got a big problem! what is the right way to compare and check two values? i written a code but it seems to be wrong.<BR><BR>&#060;p&#062;<BR>&#060;form method="post" action="check.asp"&#062;<BR>&#060;input type="text" name="one" value="0" size="3"&#062;<BR>&#060;input type="text" name="two" value="0" size="3"&#062;<BR>&#060;input type="submit" name="check"&#062;<BR>&#060;/form&#062;&#060;/p&#062;<BR>&#060;%<BR>if request.form("one") &#060; request.form("two") then<BR>response.write("&#060;p&#062;" & request.form("one") & " is smaller then "& request.form("two") & "&#060;/p&#062;")<BR>else<BR>response.write("&#060;p&#062; " & request.form("one") & " is bigger or equal to "& request.form("two") & "&#060;/p&#062;")<BR>end if<BR>%&#062;<BR><BR>i enter "1111" for the first value and "12" for the second and i got "1111 is smaller then 12".<BR><BR>any idea?

2. Senior Member
Join Date
Dec 1969
Posts
6,476

You have to convert

them to numbers before comparing...<BR><BR>Anything coming out of the request object, be it form fields or querystring<BR>are treated "Strings"<BR><BR>strings compare 1 character at a time, so for 1111, 12<BR>the first char in both is a 1 so is equal<BR>then it moves to the 2nd char to compare and sees one has ! and other is 2<BR>so it looks no further...<BR><BR>IF CLng( request.form("one") ) &#060; CLong( request.form("two") ) then<BR><BR>Or you can use<BR>CInt( request.form("one") )<BR><BR>I prefer just using CLng so it can handle pretty much any number entered error free<BR><BR>Proper way is to check to make sure they are valid numbers and that somone didnt type in "Russell" As a number and cause a mismatch error...<BR>So put them into variables...<BR><BR>lngOne = request.form("one")<BR>lngTwo = request.form("two")<BR><BR>IF isNumeric( lngOne ) Then<BR> lngOne = CLng( lngOne )<BR>End IF<BR><BR>IF isNumeric( lngTwo ) Then<BR> lngTwo = CLng( lngTwo )<BR>End IF<BR><BR>IF lngOne &#060; lngTwo Then &#039;&#060;&#060;&#060; now your good to go...<BR><BR><BR>&#039;Side note:<BR>Had you just put the values into variables without using a conversion "CLong" etc...<BR>Comparing them as variables, vbs would have been smart enough to treat them as numbers...<BR><BR>But not directly from the request object...<BR><BR>You should always always convert your data to be the proper datatypes.<BR><BR>

3. Junior Member
Join Date
Dec 1969
Posts
6

RE: You have to convert

hey thanks russel! everything is working great now!<BR><BR>note taken! thanks again pal!

Posting Permissions

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