开发者

removing IsNaN from total textbox field [duplicate]

开发者 https://www.devze.com 2023-04-12 21:08 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: When I press enter 开发者_高级运维I get isNaN, but the value is a number
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

When I press enter 开发者_高级运维I get isNaN, but the value is a number

How to change the content of a variable to zero if isNaN

When I press return "$isNaN" shows up in the textbox. I brought this problem earlier but misstated it. It's homework that I need to correct. Everything validates. I keep trying but the "$isNaN" keeps showing up. I appreciate any suggestions. Here is the link. http://ciswebs.smc.edu/cis54/tyson_schweidel/homework2.htm


You have your curly braces in the wrong place... You only check to see if the tax isNaN if the price isNaN...

The second test should not be dependent on the first... in other words even if the first test passes, you still need to run the second one (that isn't the case with your current attempt)

if (isNaN(numPrice)){ 

    alert("Sorry,you must enter a numeric value to place order");

if (isNaN(taxP))
    alert("Sorry, you must enter a numeric tax value to continue");
    return;
}

should be:

if (isNaN(numPrice)){ 

    alert("Sorry,you must enter a numeric value to place order");
    return;
}
if (isNaN(taxP)){
    alert("Sorry, you must enter a numeric tax value to continue");
    return;
}

Martin


to get a valid result your all values should be valid numeric other wise it will return NAN.

Either give a 0 as default in your all inputs like this.

<form name="form">

<p>
Price:  <input type="text" id="cost" name="cost" value="0" onchange="fixOrder();"/>
</p>
<p>
Tax: &nbsp;
<input type="text" id="tax" name="tax" value="0" onchange="fixOrder();"/>
</p>
<p>
Total: 
  <input type="text" id="total" name="total" value="0" disabled="disabled();"/>
</p>
</form>

Or use this way.

var numPrice = isNaN(parseFloat(document.getElementById("cost").value)) ? 0 :parseFloat(document.getElementById("cost").value);
    var taxP = isNaN(parseFloat(document.getElementById("tax").value)) ?0 :parseFloat(document.getElementById("tax").value) ;
    var total = isNaN(parseFloat(document.getElementById("total").value))?0 :parseFloat(document.getElementById("total").value);

by this you don't need to have any alert but if you still want to use alert than dont use this ternary just reassign 0 to your NaN value before showing alert .

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号