This is my code:
<script src="js/jquery.js" type="text/javascript"></script>
<script src="js/jquery.field.js" type="text/javascript"></script>
<script src="js/jquery.calculation.js" type="text/javascript"></script>
<script>
$(function() {
$('input[name^=sum]').keyup(function() {
var sum1 = parseFloat($('input[name=sum1]').val());
var sum2 = parseFloat($('input[name=sum2]').val());
var sum3 = parseFloat($('input[name=sum3]').val());
$('#c_card_amount').val(sum1*sum3/100);
$('#total_inc_charges').val((sum1*sum3/100)+sum1);
});
});
</script>
My questions:
-
If no value is in
sum1thec_card_amountand thetotal_inc_chargesareNaN. How can I put a condition in this that says if no value is insum1just show 0 intotal_inc_charges. I’ve read about this but I could not figure out the answer in terms of my code. -
My second question is: how can I show the numbers in 1,000.00 format for instance?
Use a short-circuit operator. If the result of
parseFloat()==NaN, the value will be set to zero.Additional Information:
See MDN Expressions and Operators