onChange problems on fields in order form

C

chumley

I have a simple orderform, whereas when I enter the quantity in the
first box, it calcuates the subtotal fine, but when i go down to the
second item and enter a quantity , the subtotal doesnt calculate, but
enters a "NaN" not a number indicator and not the calculated subtotal,
even tho i have the first product coded exactly like the second one:

function orderTotal(oform, prefix)
{
// set references to fields
var qty = oform[prefix + "_qty"];
var stHold = oform[prefix + "_stHold"];
var price = oform[prefix + "_price"];
var stVis = oform[prefix + "_stVis"];

// only bother if the field has contents
if (qty == "")return;

// if the with is not a number (NaN)
// or is zero or less
// everything goes blank
if(isNaN(qty.value) || (qty.value <= 0))
{
qty.value = "";
stHold.value = "";
}

// else the field is a valid number, so calculate the
// total order cost and put that value in the
// hidden subtotal field
else

{
stHold.value = (Math.round(qty.value * price.value * 100))/100;
}
// call the routine which checks if the
// visible subtotal is correct
visTotal(oform, prefix);
}

// checks if the visible subtotal is correct
// ie, if it equals the hidden subtotal field
function visTotal(oform, prefix)
{
var stHold = oform[prefix + "_stHold"];
var stVis = oform[prefix + "_stVis"];

if (stVis.value != stHold.value)
stVis.value = stHold.value;
}
/////////html
<form>
<INPUT
TYPE=TEXT
NAME="vn_qty"
SIZE=3
onChange="orderTotal(this.form, 'vn')"<INPUT TYPE=HIDDEN NAME="vn_price" VALUE="270.00">$270.00

<INPUT TYPE=HIDDEN NAME="vn_stHold">
<INPUT
TYPE=TEXT
NAME="vn_stVis"
SIZE=10
onChange="visTotal(this.form, 'vn')"<!--second input box, this one is the one not working -->
<INPUT
TYPE=TEXT
NAME="ja_qty"
SIZE=3
onChange="orderTotal(this.form, 'ja')"<INPUT TYPE=HIDDEN NAME="ja_price" VALUE="195.00">$195.00
<INPUT TYPE=HIDDEN NAME="ja_stHold">
<INPUT
TYPE=TEXT
NAME="ja_stVis"
SIZE=10
onChange="visTotal(this.form, 'ja')"

???
chumley
 
R

RobG

I have a simple orderform, whereas when I enter the quantity in the
first box, it calcuates the subtotal fine, but when i go down to the
second item and enter a quantity , the subtotal doesnt calculate, but
enters a "NaN" not a number indicator and not the calculated subtotal,
even tho i have the first product coded exactly like the second one:

It is much better if you post code that can be simply copied and
pasted into a browser and it "works". Others shouldn't have to do
anything to get it to run, make it easy for us! Also, we may be fixing
the error in getting it to run without realising it.

I wrapped your script in script tags and put into a page - as far as I
can tell it works.
function orderTotal(oform, prefix)
{
// set references to fields
var qty = oform[prefix + "_qty"];
var stHold = oform[prefix + "_stHold"];
var price = oform[prefix + "_price"];
var stVis = oform[prefix + "_stVis"];

// only bother if the field has contents
if (qty == "")return;

// if the with is not a number (NaN)
// or is zero or less
// everything goes blank

Don't do that, it's bad UI design. Expect that users will try to put
something useful into those fields - if they get it wrong, you remove
the content so they can't see what the issue was and have to re-enter
all characters. Tell them about the error (e.g. "Quantity must be a
whole number greater than zero") and let them fix it however they
wish. On-screen hints regarding format can help.

if(isNaN(qty.value) || (qty.value <= 0))

If you want to test that a string meets certain criteria, a regular
expression is usually preferred. There are some good hints here about
validating input (including numbers):

{
qty.value = "";
stHold.value = "";
}

// else the field is a valid number, so calculate the
// total order cost and put that value in the
// hidden subtotal field
else

{
stHold.value = (Math.round(qty.value * price.value * 100))/100;}

If you're trying to format a number with two decimal places, that's a
pretty awful way to do it. Try the FAQ:

5.1 How do I format a Number as a String with exactly 2 decimal
places?
// call the routine which checks if the
// visible subtotal is correct
visTotal(oform, prefix);

Seems to me it should just set the value.

[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top