compare comes back wrong 
March 1st, 2007, 06:15 PM
| | | |
I am comparing two numbers, and the comparison gives absolutely bizzare
results. I am comparing 5 with a number that is entered in a form. Then
if the number entered is 5, or between 50 and 95, it gives the correct
answer, but if it is 10 to 45, or over 99, it does not. What the heck
is it doing here?
Here is the code:
if (form.quantity.value < form.minimum.value) printerror = printerror +
"\nThere is a minimum quantity of " + form.minimum.value;
form.minimum.value is set to 5. form.quantity.value is set according to
what is entered in the form.
It appears to be doing a string comparison on the numbers, but all the
documentation I find says that it should be doing a numeric computation.
Marshall | 
March 1st, 2007, 06:35 PM
| | | | re: compare comes back wrong
On Mar 1, 9:06 pm, Marshall Dudley <mdud...@king-cart.comwrote: Quote:
if (form.quantity.value < form.minimum.value) printerror = printerror +
"\nThere is a minimum quantity of " + form.minimum.value;
>
form.minimum.value is set to 5. form.quantity.value is set according to
what is entered in the form.
>
It appears to be doing a string comparison on the numbers, but all the
documentation I find says that it should be doing a numeric computation.
| A string compared with another string gives alphabetic comparison
results. What documentation does say opposite?
if (+form.quantity.value < +form.minimum.value) is the simplest way to
get numeric comparison. | 
March 1st, 2007, 06:45 PM
| | | | re: compare comes back wrong
Marshall Dudley wrote: Quote:
I am comparing two numbers, and the comparison gives absolutely bizzare
results. I am comparing 5 with a number that is entered in a form. Then
if the number entered is 5, or between 50 and 95, it gives the correct
answer, but if it is 10 to 45, or over 99, it does not. What the heck
is it doing here?
>
Here is the code:
>
if (form.quantity.value < form.minimum.value) printerror = printerror +
"\nThere is a minimum quantity of " + form.minimum.value;
>
| if (+form.quantity.value < +form.minimum.value) printerror = printerror + Quote: |
"\nThere is a minimum quantity of " + form.minimum.value;
| You should check that the input is indeed a number:
if (isNaN(form.quantity.value)||
(+form.quantity.value < +form.minimum.value)){
printerror = printerror +
"\nThere is a minimum quantity of " + form.minimum.value;
}
Mick Quote:
form.minimum.value is set to 5. form.quantity.value is set according to
what is entered in the form.
>
It appears to be doing a string comparison on the numbers, but all the
documentation I find says that it should be doing a numeric computation.
>
Marshall
| | 
March 1st, 2007, 06:45 PM
| | | | re: compare comes back wrong
Marshall Dudley said: Quote:
>
>I am comparing two numbers, and the comparison gives absolutely bizzare
>results. I am comparing 5 with a number that is entered in a form. Then
>if the number entered is 5, or between 50 and 95, it gives the correct
>answer, but if it is 10 to 45, or over 99, it does not. What the heck
>is it doing here?
>
>Here is the code:
>
>if (form.quantity.value < form.minimum.value) printerror = printerror +
>"\nThere is a minimum quantity of " + form.minimum.value;
>
>form.minimum.value is set to 5. form.quantity.value is set according to
>what is entered in the form.
>
>It appears to be doing a string comparison on the numbers, but all the
>documentation I find says that it should be doing a numeric computation.
| Throw away all the documentation you found. It's all wrong.
Form control values are *always* strings.
-- | 
March 1st, 2007, 06:45 PM
| | | | re: compare comes back wrong
On Mar 1, 10:06 am, Marshall Dudley <mdud...@king-cart.comwrote: Quote:
I am comparing two numbers, and the comparison gives absolutely bizzare
results. I am comparing 5 with a number that is entered in a form. Then
if the number entered is 5, or between 50 and 95, it gives the correct
answer, but if it is 10 to 45, or over 99, it does not. What the heck
is it doing here?
>
Here is the code:
>
if (form.quantity.value < form.minimum.value) printerror = printerror +
"\nThere is a minimum quantity of " + form.minimum.value;
>
form.minimum.value is set to 5. form.quantity.value is set according to
what is entered in the form.
>
It appears to be doing a string comparison on the numbers, but all the
documentation I find says that it should be doing a numeric computation.
>
Marshall
| Don't know what documentation you're looking at, but you need to
convert your strings to numbers before doing a comparision:
var qty = +form.quantity.value, min = +form.minimum.value;
// more verbose, but equivalent:
var qty = parseInt(form.quantity.value, 10), min =
parseInt(form.quantity.value, 10);
if (qty < min) printerror += '\nThere is a minimum quantity of ' +
min;
-David | 
March 1st, 2007, 07:15 PM
| | | | re: compare comes back wrong
VK wrote: Quote:
On Mar 1, 9:06 pm, Marshall Dudley <mdud...@king-cart.comwrote:
> Quote:
>if (form.quantity.value < form.minimum.value) printerror = printerror +
>"\nThere is a minimum quantity of " + form.minimum.value;
>>
>form.minimum.value is set to 5. form.quantity.value is set according to
>what is entered in the form.
>>
>It appears to be doing a string comparison on the numbers, but all the
>documentation I find says that it should be doing a numeric computation.
>>
| >
A string compared with another string gives alphabetic comparison
results. What documentation does say opposite?
>
| Who's comparing strings? I am comparing numeric values. 5 and 95 are
numbers. Quote:
if (+form.quantity.value < +form.minimum.value) is the simplest way to
get numeric comparison.
>
>
>
| I will try taht and see if it works.
Thanks,
Marshall | 
March 1st, 2007, 08:25 PM
| | | | re: compare comes back wrong
On Mar 1, 10:01 pm, Marshall Dudley <mdud...@king-cart.comwrote: Quote: |
Who's comparing strings? I am comparing numeric values.
| You are comparing two form input field values. Any form input value
for JavaScript is a Unicode string unless you explicetly instructed it
to think something else.
In case like
if (myForm.inputField.value 5)
JavaScript will try to convert the left string argument into number
In case like
if (myForm.inputField1.value myForm.inputField2.value)
it has no reason to make any conversions. The greater-than sign is
used for both alphabetical and numerical comparisons, and if both
arguments are strings then the engine's AI doesn't go so far to study
strings' content for some educated guess.
In many languages - and for a reason - there are two set of operators
for numerical and alphabetical comparison like / gt, < / lt etc.
JavaScript is not one of them, so it needs your help if both arguments
are strings but you really want to compare them as numbers. | 
March 2nd, 2007, 09:45 AM
| | | | re: compare comes back wrong
On Mar 1, 7:28 pm, "VK" wrote: Quote:
if (+form.quantity.value < +form.minimum.value) is the
simplest way to get numeric comparison.
| I love this place. Although I knew what was wrong
in what he was doing, and what the solution was
(to convert to numbers), I didn't realize you can
just put a + in front of it. So that's a direct
replacement for wrapping a string that represents
a number in parseInt( ) ? I'd love to go through
all my code and replace them with a + (carefully by
hand of course, I wouldn't do a global replace, it
would be far too dangerous).
btw, while I'm here, anyone know why every time I
put a trimmed quote in my posts, it never gets
collapsed with a - Show quoted text - link like
all y'alls do ? | 
March 2nd, 2007, 09:55 AM
| | | | re: compare comes back wrong
"VK" <schools_ring@yahoo.comwrote: Quote:
In many languages - and for a reason - there are two set of operators
for numerical and alphabetical comparison like / gt, < / lt etc.
JavaScript is not one of them, so it needs your help if both arguments
are strings but you really want to compare them as numbers.
| Building on your original suggestion, but recognising that you only have to
explicitly force one side of the comparison to a numeric value:
If you wanted numeric comparison operators you could always pretend that
they are spelled "<+", ">+", etc. true
BTW, you were wrong to say "A string compared with another string gives
alphabetic comparison results". String comparison compares unicode
character codepoints. So as soon as you go beyond simple ascii strings they
no longer compare alphabetically: true | 
March 2nd, 2007, 11:25 AM
| | | | re: compare comes back wrong
On Mar 2, 9:38 am, dd wrote: Quote:
On Mar 1, 7:28 pm, VK wrote:
> Quote:
if (+form.quantity.value < +form.minimum.value) is the
simplest way to get numeric comparison.
| >
I love this place. Although I knew what was wrong
in what he was doing, and what the solution was
(to convert to numbers), I didn't realize you can
just put a + in front of it.
| The group's FAQ says that is so, and goes into details. Quote:
So that's a direct
replacement for wrapping a string that represents
a number in parseInt( ) ?
| No. The - parseInt - function parses a string as an integer, and
throws away any trailing part of a string that cannot be interpreted
as part of an integer. Thus a string representation of a number with a
decimal fractional part will be truncated at the decimal point (and
representations of numbers with exponents get very unhelpful
handling). The - parseFloat - function would be a nearer comparison,
but the unary plus operator actually acts to type-convert from string
to number using type-conversion rules, so the whole string is
considered and the handling of strings that are not representations of
numbers is different in some cases. Quote:
I'd love to go through
all my code and replace them with a + (carefully by
hand of course, I wouldn't do a global replace, it
would be far too dangerous).
| Where that change is appropriate the rustles would be 7-14 times
faster. Quote:
btw, while I'm here, anyone know why every time I
put a trimmed quote in my posts, it never gets
collapsed with a - Show quoted text - link like
all y'alls do ?
| Questions about the behaviour of particular web Usenet interfaces/news
readers should be directed to those responsible (or at least openly
interested).
Richard. | 
March 2nd, 2007, 11:35 AM
| | | | re: compare comes back wrong
On Mar 2, 9:46 am, Duncan Booth wrote:
<snip> Quote:
BTW, you were wrong to say "A string compared with another string
gives alphabetic comparison results". String comparison compares
unicode character codepoints. So as soon as you go beyond simple
ascii strings they no longer compare alphabetically:
| Where is the need to go beyond ascii?
"5" < "40"
- is already non-alphabetic, but the characters are all ascii.
Richard. | 
March 2nd, 2007, 05:15 PM
| | | | re: compare comes back wrong
In comp.lang.javascript message <45e71c39$0$28099$4c368faf@roadrunner.co
m>, Thu, 1 Mar 2007 13:32:24, Michael White <mick@mickweb.composted: Quote:
>
>You should check that the input is indeed a number:
>if (isNaN(form.quantity.value)||
| Yes, but that is a weak check. In many cases, numbers such as
Infinity -3 4.5 1e88 are clearly inappropriate regardless of the
numeric value, and an order quantity of 2000e-3 need not be accepted.
Generally, one can choose between allowing any input and validating
elsewhere, allowing any input and letting the user take the
consequences, or using a RegExp to constrain the input pattern quite
strongly.
Temp = form.quantity.value
Fail = /\D/.test(Temp) // ...
Nmbr = +Temp
--
(c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News. |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 225,702 network members.
|