Solved

NaN Error in Subtotal Field

Posted on 2010-09-05
14
570 Views
Last Modified: 2013-11-19
Hi everyone,

I'm writing because I've been working to code a fairly simple order form and while I've pretty much gotten everything down, I'm having an issue with getting NaN errors if the discount field on the form does not contain a value.

For example, the form starts out with 0 in the field, and if a user changes the value, the amount is deducted from the total, however if a user erases the 0 or value and leaves the field blank, the subtotal column will display an NaN error.

I've tried sorting through the code and putting in code to treat the discount field as 0 if it is NaN however no matter what I try I haven't been able to solve the issue.

I've attached the code, however it is fairly lengthy mainly due to the fact that the item listings are repeated ten times. The main code to focus on is really at the top near the head (in the script field) and probably the lines after the last item listing where you see Sales Tax and such.

Also, the current version of the form is at:  http://intensivejournal.org/info/buyItems.php

Thanks very much in advance for any assistance.
buyItems.php
0
Comment
Question by:parajrn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 9

Expert Comment

by:Valliappan AN
ID: 33607345
When I tried the online form you gave, its error occurring on Shipping and Additional Shipping fields when the 0 is deleted, the NaN error occurs. Basically check for NaN and empty string for all such fields, and you get the results.

Have a look at:
http://www.webdeveloper.com/forum/showthread.php?t=27169

Thanks.
0
 
LVL 1

Expert Comment

by:dolomitedave
ID: 33607452
sorry i havent had time to look into this one in depth but you could try using parseInt(var) on your discount variable or textbox value ie

var newvar = parseInt(document.getElemementById('discount').value)
or
nevar = parseInt(oldvar)
i am sure your get the idea, hope this helps
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33607504
var subTotalOne = document.scriptForm.subTotal1.value;
var shipping = document.scriptForm.shipping.value;
var addShipping = document.scriptForm.addShipping.value;

subTotalOne = isNaN(subTotalOne)?0:parseInt(subTotalOne,10)
shipping = ...
addShipping = ...
 
 

var subTotalTwo = subTotalOne - discountTemp + shipping + addShipping;


0
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
LVL 1

Expert Comment

by:dolomitedave
ID: 33607535
i just noticed something

should this
function discountErr()
      {
            var discountTemp = "0";
be this
function discountErr()
      {
            var discountTemp = 0;

i allways thought if you use double quotes when defining a variable you define it as a string  if you don't then it's defined as a number so when trying to calculate it is subtracting a string not a number hence the error.

worth a try
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33609085
Definitely ALSO a problem!
0
 

Author Comment

by:parajrn
ID: 33649166
Hi Everyone,

Thanks very much for the input. I tried some of the solutions and they seem to be working however I'm having an issue where if a user fills in rows 1, 2, 3... etc, in order you'll get a valid total, however if a user were to skip rows around, then you get a totally different figure.

I'm not sure if this is due to where I'm placing the tax function or how I'm converting to decimals, but if it's permissible to ask this in the thread, is there a more precise addition method I can use so that regardless of row order, you'll get the same calculations regardless?

Thanks again very much and apologies for the late reply. I've been having a very busy holiday week.
0
 
LVL 1

Expert Comment

by:dolomitedave
ID: 33649827
hi

i think this

<Input type=Text Name="quantity2" size = 2 onChange="document.scriptForm.totalCost2.value = calcTotal(document.scriptForm.quantity2.value, document.scriptForm.unitPrice2.value); calcGrandTotal2();">

should be this

<Input type=Text Name="quantity2" size = 2 onChange="document.scriptForm.totalCost2.value = calcTotal(document.scriptForm.quantity2.value, document.scriptForm.unitPrice2.value); ">

0
 

Author Comment

by:parajrn
ID: 33668896
Hi dolo,

Thanks for the info but I tried making the correction which can be seen on the sites current form but I'm still experiencing the issue. I'll try to have a more in depth look myself tomorrow however any additional insights are welcome.
0
 

Author Comment

by:parajrn
ID: 33707834
Hi Everyone,

I don't know why I didn't think of this earlier, but is there a snipplet of code I could use to require the user to enter the items in order rather than skip around, but also be able to fill in the discount field and such even if not all 10 item fields are filled in?
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 33708074
Add taborder to the form
0
 

Author Comment

by:parajrn
ID: 33755756
Hi mplungjan,

Thanks very much for the info. Many apologies for the late reply, but things have been very hectic on my end. I'll try implementing that solution and post an update shortly or if something comes up, than at most by tomorrow afternoon
0
 

Author Comment

by:parajrn
ID: 33756011
I just had a look at the Taborder functionality but it appears to be limited to .net languatges. Is there anything that would work with Javascript?
0
 
LVL 75

Accepted Solution

by:
Michel Plungjan earned 500 total points
ID: 33756291
0
 

Author Closing Comment

by:parajrn
ID: 34105194
Thanks very much for your assistance, it is very much appreciated!
0

Featured Post

Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Get to know the ins and outs of building a web-based ERP system for your enterprise. Development timeline, technology, and costs outlined.
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question