Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Nov 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    adding text boxes

    I'm doing a school project and I'm trying to add up several text boxes, however some of them are blank and when I calculate them I get a NaN result. Here is my code:

    qty1= parseFloat(document.form1.qty_prt.value)
    qty2= parseFloat(document.form1.qty_mon.value)
    qty3= parseFloat(document.form1.qty_comp.value)
    sub_tot=parseFloat(document.form1.subt.value)
    tax_tot=parseFloat(document.form1.tax.value)
    grand_tot=parseFloat(document.form1.grandt.value)
    qty_tot=eval(qty1+qty2+qty3)
    alert("quantity is " + qty_tot)

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    isNaN() is a function provided for just such things.
    http://www.devguru.com/Technologies/...ref/isnan.html

    Not sure if there is a better way, but I'd just test each one first.
    e.g.
    qty1= isNaN(parseFloat(document.form1.qty_prt.value))?0:document.form1.qty_prt.value

    You don't need eval. Eval is Evil.
    qty_tot=qty1+qty2+qty3

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New Coder
    Join Date
    Nov 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Okay I get what you're doing in the forst part qty1= isNaN(parseFloat(document.form1.qty_prt.value)) your converting the value if any to numeric value and checking if it is numeric(I think).
    However what is the second part ?0:document.form1.qty_prt.value?

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    That's shortcut syntax for an if/then when doing an assignment.

    i.e.
    Code:
    int c = 0;
    if (b == 1)
    {
       c = 2;
    }
    else
    {
       c = 5;
    }
    Can be written as
    int c = b==1 ? 2 : 5;

    So this (note the 2nd parseFloat i forgot, actually):
    Code:
    qty1= isNaN(parseFloat(document.form1.qty_prt.value))?0:parseFloat(document.form1.qty_prt.value);
    is a shortcut for this:
    Code:
    if (isNaN(parseFloat(document.form1.qty_prt.value)))
    {
       qty1 = 0;
    }
    else
    {
       qty1 = parseFloat(document.form1.qty_prt.value)
    }
    Last edited by nikkiH; 12-14-2006 at 07:56 PM. Reason: i can't type

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #5
    New Coder
    Join Date
    Nov 2006
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the explaination, I'll give it a try.


  •  

    Posting Permissions

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