9

What I'm trying to do:

I have a javascript program that, when a button is clicked, takes in 4 strings from 4 text boxes in a form, and outputs those strings into a formatted textarea.

function testResults(form){
var errorhandle1 = parseInt(document.myForm.Item_Code.value);
var errorhandle2 = parseInt(document.myForm.Item_Cost.value);
var errorhandle3 = parseInt(document.myForm.Quantity.value);
//above variables are for error handling.
var d = "   ";
var subtotal = parseInt(form.Item_Cost.value) * parseInt(form.Quantity.value);
var subtotalValue = parseInt(document.myForm.Subtotal.value);
var testVar = "Item Code: " + form.Item_Code.value + d + 
              "Item Name: " + form.Item_Name.value + d + 
              "Item Cost: " + form.Item_Cost.value + d + 
              "Quantity: " + form.Quantity.value + '\n';


document.myForm.myTextarea.value += testVar;
document.myForm.Subtotal.value = parseInt(subtotal) + subtotalValue;
document.myForm.Sales_Tax.value =  document.myForm.Subtotal.value * salestax;
document.myForm.Total.value = parseInt(document.myForm.Subtotal.value) + parseFloat(document.myForm.Sales_Tax.value);
}

The above code works just fine, and does exactly what I want it to do for the scope of my program.

try {
    if ((isNaN(errorhandle3) == true) || (isNaN(errorhandle2) == true)) {
        throw "Error1";
    }
} catch (e) {
    if (e == "Error1") {
        alert("Error! You must enter a number into the qty and cost fields!");
    }
}

What I'm trying to accomplish with the try...catch block is simply to make sure that

document.myForm.Item_Code.value
document.myForm.Item_Cost.value
document.myForm.Quantity.value

are actually numbers.

The try...catch statements trigger every time I run the program and doesn't care what I put in the corresponding text boxes. I would greatly appreciate any and all insight on this!

Also: I looked at both of these links and was unable to understand my problem. javascript parseInt return NaN for empty string http://www.w3schools.com/jsref/jsref_isnan.asp

Community
  • 1
  • 1
Fishbones
  • 103
  • 1
  • 1
  • 4
  • 4
    This makes no sense here to use `try/catch` around something this simple that doesn't by itself throw. You can just use a straight `if` statement and the code will be simpler and perform better. I don't see any need for exceptions here. – jfriend00 Dec 04 '14 at 01:56
  • 2
    Tip: don’t test booleans against `== true`. It’s redundant; `if (isNaN(errorhandle3) || isNaN(errorhandle2))` reads better. – Ry- Dec 04 '14 at 02:00
  • Thank you both for the quick reply. I'm in the process of changing those now. – Fishbones Dec 04 '14 at 02:09

2 Answers2

15

Your root problem here is that isNaN() tests to see if the value is NaN. It does not test to see if a string is a proper number. It has some coercion rules to try to deal with strings, but that really isn't what it is designed for.

You can see ways to test if something can be parsed into a valid number here: Validate decimal numbers in JavaScript - IsNumeric()

It's worth reading the detail in the good answers there, but it boils down to something like this which is a bit more than you need, but is general purpose:

function isNumber(n) {
  return !isNaN(parseFloat(n)) && isFinite(n);
}

And, then there's no reason to use exceptions in your code, so you can just do this:

if (!isNumber(errorhandle3) || !(isNumber(errorhandle2)) {
   alert("Error! You must enter a number into the qty and cost fields!");
}

Also, in your code, some .Value properties look like maybe they should be .value (lowercase).

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979
0

In your first code block

var errorhandle2 = parseInt(document.myForm.Item_Cost.Value);
var errorhandle3 = parseInt(document.myForm.Quantity.Value);

You are using Value, which should be value, that's case-sensitive.

By the way, isNaN returns boolean, you don't have to compare with true

Leo
  • 13,428
  • 5
  • 43
  • 61