4 Replies - 1824 Views - Last Post: 28 July 2006 - 01:40 AM

#1 wzeller  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 82
  • Joined: 09-April 06

Javascript, if x>y only working when x only a little greater

Posted 26 July 2006 - 08:49 PM

Hello All,

I'm hoping somebody can tell me what's going on here, because to me it seems totally bizarre.

I am dynamically generating some javascript code from a php script. One part of it is some data validation.

It's an order form named "fulfill", and the script checks the quantities ordered of each item and compares it to the maximum order size for that item and sets a span contents to 'EXCEEDED' when the quantity being ordered is too high. (This gets called via onchange on the quantity fields.)

The name of the quantity field where a user enters a number is sku<itemcode> (that makes more sense when you look at the code below). The maximum allowed is stored in a hidden field called m<last4digitsOfSku>, and the span whose contents are set is x<last4digitsOfSku>.

So, here's a snippet the code being generated:
if (document.fulfill.sku689076715898.value > document.fulfill.m5898.value) { document.getElementById('x5898').innerHTML="EXCEEDED"; }
	 else { document.getElementById('x5898').innerHTML=" "; }


There are about fifteen of those statements, each with a different SKU.

Now, here's what's weird: It works fine, as long as the quantity entered is only a little higher than the maximum.

For instance, if the maximum is 250 and the person enters 300, then the span contents get set to "EXPIRED" just as expected. But if they enter 950 then the span is left alone.

All 15 of them are behaving the same way.

In the meantime, another part of the same validation script is handling the bigger numbers just fine, so it doesn't seem to be having a problem evaluating the larger numbers.

None of the SKU numbers have the same last four digits as any other SKU number, so there shouldn't be any field name conflicts. (And if there were, then it would effect small numbers as well as the big ones.)

I'm at a loss.

Any ideas?

Wayne

Is This A Good Question/Topic? 0
  • +

Replies To: Javascript, if x>y only working when x only a little greater

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Javascript, if x>y only working when x only a little greater

Posted 26 July 2006 - 09:40 PM

You may want to the parse the value returned to an integer type as I believe when you retrieve a value from a field using the .value property it is returned as a string.
Was This Post Helpful? 0
  • +
  • -

#3 Spider  Icon User is offline

  • Arachnid

Reputation: 2
  • View blog
  • Posts: 769
  • Joined: 10-July 02

Re: Javascript, if x>y only working when x only a little greater

Posted 27 July 2006 - 06:21 AM

I believe the simplest way to solve your problem is to do this for each value:

Number(document.fulfill.sku689076715898.value)

Was This Post Helpful? 0
  • +
  • -

#4 wzeller  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 82
  • Joined: 09-April 06

Re: Javascript, if x>y only working when x only a little greater

Posted 27 July 2006 - 03:38 PM

View PostSpider, on 27 Jul, 2006 - 06:21 AM, said:

I believe the simplest way to solve your problem is to do this for each value:

Number(document.fulfill.sku689076715898.value)



Thank you!

That fixed it instantly.

What a weird way for JS to interpret things: "If the difference between two numbers is more than about a hundred or so, then they must be strings instead of numbers."

Odd.

Anyway, thanks to your simple fix I have a working page and a new bit of knowledge. :)

Wayne
Was This Post Helpful? 0
  • +
  • -

#5 Spider  Icon User is offline

  • Arachnid

Reputation: 2
  • View blog
  • Posts: 769
  • Joined: 10-July 02

Re: Javascript, if x>y only working when x only a little greater

Posted 28 July 2006 - 01:40 AM

View Postwzeller, on 27 Jul, 2006 - 11:38 PM, said:

What a weird way for JS to interpret things: "If the difference between two numbers is more than about a hundred or so, then they must be strings instead of numbers."


That isn't what happens at all, when you initially get the values from the page, they are strings not integers. Almost all document.*.value things return a string because that is the way they are processed on a web page.

The result just happened to be correct for smaller differences because of the way string comparisons work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1