if () else problem

# Thread: if () else problem

1. Senior Member
Join Date
Dec 1969
Posts
2,892

## if () else problem

Hi all:<BR><BR>I have this function which works fine as long as the user doesn&#039;t enter 0 as the qty. I try to test for it and handle it accordingly, but nothing happens when a 0 is entered.<BR><BR>Is there some special thing I need to know about the parseFloat function that may explain this?<BR><BR>function calcSubTot(intFieldCounter, unitPrice, qty){<BR> if (intFieldCounter.value != ""){<BR> if (parseFloat(qty) != 0){<BR> if ( isNaN(qty) ){<BR> qty = 0;<BR> alert("Please enter only numbers in the quantity field.");<BR> //return false;<BR> window.document.form_OrderForm.elements[&#039;frmQty&#039; + intFieldCounter].focus(); <BR> }<BR> unitPrice = parseFloat( unitPrice );<BR> //alert("DEBUG: unitPrice after parseFloat = " + unitPrice);<BR><BR> var denom = "\$"; <BR><BR> subTot = (unitPrice * qty);<BR> //alert("DEBUG: subTot after basic calc = " + subTot);<BR> <BR> subTot = Math.round( subTot * 100 );<BR> //alert("DEBUG: subTot, after Math.round(*100) = " + subTot);<BR><BR> if ( subTot &#060; 10 ) {<BR> subTot = denom + "0.0" + subTot; <BR> //alert("DEBUG: subTot was &#060; 10 so now it&#039;s = " + subTot);<BR> }<BR> if ( subTot &#060; 100 ){<BR> subTot = denom + "0." + subTot; <BR> //alert("DEBUG: subTot was &#060; 100 so now it&#039;s = " + subTot);<BR> } <BR> subTot = denom + subTot; // convert amt to string <BR> subTot = subTot.substring(0, subTot.length -2) + "." + subTot.substring(subTot.length -2); <BR> <BR> window.document.form_OrderForm.elements[&#039;frmLineSubtot&#039; + intFieldCounter].value = subTot; <BR> } //if ( isNaN(qty) ){<BR> } //(qty != 0){<BR> else {<BR> alert("qty = 0!");<BR> window.document.form_OrderForm.elements[&#039;frmLineSubtot&#039; + intFieldCounter].value = "-"; <BR> }<BR>} //(intFieldCounter.value != ""){<BR>

2. Senior Member
Join Date
Dec 1969
Posts
96,118

You check to see if the value is zero and *THEN* check to see if it is NaN!!!<BR><BR>If it *is* NaN, then it will *never* be zero! NaN and zero are *not* the same.<BR><BR>Try something like this:<BR><BR>function calcSubTot(intFieldCounter, unitPrice, qty)<BR>{ <BR> // save code...shower with a friend<BR> var frm = window.document.form_OrderForm;<BR><BR> if (intFieldCounter.value != "")<BR> {<BR> qty = parseFloat(qty);<BR> if ( isNaN(qty) )<BR> {<BR> qty = 0;<BR> alert("Please enter only numbers in the quantity field.");<BR> frm.elements[&#039;frmQty&#039; + intFieldCounter].focus(); <BR> // the return has to be *AFTER* the focus call, of course!<BR> return false; // why did you comment out the return?<BR> }<BR> if ( qty &#062; 0 ) <BR> {<BR> ...<BR> } else {<BR> alert("qty = 0!");<BR> frm.elements[&#039;frmLineSubtot&#039; + intFieldCounter].value = "-"; <BR> ??? aren&#039;t you going to focus and return false here ???<BR> }<BR> }<BR>}<BR>

3. Senior Member
Join Date
Dec 1969
Posts
2,892

## RE: Your code is backwards!

AH! That makes sense! D&#039;OH on me.<BR><BR>Thanks Bill, I&#039;m modifying it now...

4. Senior Member
Join Date
Dec 1969
Posts
2,892

## Hey Bill...

... I added a section of code to loop through the qty fields and test for a value, then calculate a subtotal. But I broke my toy. Can I loop through just form fields by name, or do I have to do the entire collection?<BR><BR>function calcSubTot(intFieldCounter, unitPrice, qty){<BR> var orderForm = window.document.form_OrderForm;<BR> <BR> if (intFieldCounter.value != ""){<BR> qty = parseFloat(qty); <BR> <BR> if ( isNaN(qty) ){<BR> qty = 0; <BR> alert("Please enter only numbers in the quantity field."); <BR> orderForm.elements[&#039;frmQty&#039; + intFieldCounter].focus(); <BR> return false;<BR> } <BR> if ( qty &#062; 0 ){<BR> unitPrice = parseFloat( unitPrice );<BR> //alert("DEBUG: unitPrice after parseFloat = " + unitPrice);<BR><BR> var denom = "\$"; <BR><BR> subTot = (unitPrice * qty);<BR> //alert("DEBUG: subTot after basic calc = " + subTot);<BR> <BR> subTot = Math.round( subTot * 100 );<BR> //alert("DEBUG: subTot, after Math.round(*100) = " + subTot);<BR><BR> if ( subTot &#060; 10 ) {<BR> subTot = denom + "0.0" + subTot; <BR> //alert("DEBUG: subTot was &#060; 10 so now it&#039;s = " + subTot);<BR> }<BR> if ( subTot &#060; 100 ){<BR> subTot = denom + "0." + subTot; <BR> //alert("DEBUG: subTot was &#060; 100 so now it&#039;s = " + subTot);<BR> } <BR> subTot = denom + subTot; // convert amt to string <BR> subTot = subTot.substring(0, subTot.length -2) + "." + subTot.substring(subTot.length -2); <BR> <BR> orderForm.elements[&#039;frmLineSubtot&#039; + intFieldCounter].value = subTot; <BR> <BR> [hl="yellow"]//Now lets loop the elements array and build up the formSubTotal value.<BR> var formSubTotal = 0;<BR> <BR> for (var i = 0; i &#060; orderForm.elements.length; i++){<BR> if ( orderForm.elements.[&#039;frmQty&#039; + i].value != "" ){ // if the qty field isn&#039;t empty...<BR> formSubTotal += orderForm.elements.[&#039;frmQty&#039; + i].value;<BR> }<BR> }// for (var i = 0; i &#060; orderForm.elements.length; i++){<BR> <BR> orderForm.frmSubtot.value = formSubTotal;(I know this doesn&#039;t actually calculate a subtotal, I just wanted some math for a test.)[/hl] <BR> } <BR> else {<BR> alert("You entered 0 as your quantitiy."); <BR> orderForm.elements[&#039;frmLineSubtot&#039; + intFieldCounter].value = "-"; <BR> orderForm.elements[&#039;frmQty&#039; + intFieldCounter].focus(); <BR> return false; <BR> } //if ( qty &#062; 0 ){<BR> } // if (intFieldCounter.value != ""){<BR>} //calcSubTot(intFieldCounter, unitPrice, qty){<BR>

5. Senior Member
Join Date
Dec 1969
Posts
2,892

## Got it - at least...

... this section. Thanks.

#### Posting Permissions

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