Solved

NaN Error in Subtotal Field

Posted on 2010-09-05
14
567 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
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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

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…
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.

839 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