onChange problems on fields in order form

Discussion in 'Javascript' started by chumley, Jan 19, 2010.

  1. chumley

    chumley Guest

    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

    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;
    onChange="orderTotal(this.form, 'vn')"<INPUT TYPE=HIDDEN NAME="vn_price" VALUE="270.00">$270.00

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

    chumley, Jan 19, 2010
    1. Advertisements

  2. chumley

    RobG Guest

    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.
    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 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):

    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
    Seems to me it should just set the value.

    RobG, Jan 20, 2010
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.