Solved

NaN Error in Subtotal Field

Posted on 2010-09-05
14
557 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
  • 6
  • 4
  • 3
  • +1
14 Comments
 
LVL 9

Expert Comment

by:Valliappan AN
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 1

Expert Comment

by:dolomitedave
Comment Utility
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
Comment Utility
Definitely ALSO a problem!
0
 

Author Comment

by:parajrn
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:parajrn
Comment Utility
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
Comment Utility
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
Comment Utility
Add taborder to the form
0
 

Author Comment

by:parajrn
Comment Utility
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
Comment Utility
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
Comment Utility
0
 

Author Closing Comment

by:parajrn
Comment Utility
Thanks very much for your assistance, it is very much appreciated!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to dynamically set the form action using jQuery.

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now