2017-11-14 17 views
0

計算後にテキストボックスからNaNを削除する必要があるjavascript関数がありますか?計算中に入力からNaNを削除します

function calculateInterest() { 

    var repay_period = parseInt(document.getElementById("emp_loan_repayment_method_id").value); 
    var loan_amount = parseInt(document.getElementById("maximum_loan_amount").value); 
    var rate_of_interest = parseInt(document.getElementById("rate_of_interest").value); 
    var no_of_months = parseInt(document.getElementById("repayment_periods").value); 
    var repay_month_amount = parseInt(document.getElementById("repayment_amount").value); 
    var no_of_periods = no_of_months/12; 

    { 
     var payable_interest = (loan_amount * no_of_periods * rate_of_interest)/100; 

     var pay_interest = payable_interest.toFixed(2); 

     document.getElementById("total_payable_interest").value = pay_interest; 


     var repay_amount = (loan_amount/no_of_months).toFixed(2); 

     document.getElementById("repayment_amount").value = repay_amount; 

     var total_payable_amount = (payable_interest + loan_amount).toFixed(2); 

     document.getElementById("total_payable_amount").value = total_payable_amount; 


    } 
} 
+0

チェック値が数値であるならば、 –

+0

することができます....入力にそれを解析:

はスニペット以下を参照してください以下に述べるように 'isNaN()'を使用してください – Niladri

+0

提供されているソリューションがあれば、それを受け入れてください – mega6382

答えて

0

テキストボックスに値を割り当てることがNaNであるかどうかをチェック:

if(!isNaN(parseInt(value_to_test))) 
    document.getElementById("yout_input").value = value_to_test; 
else 
    document.getElementById("yout_input").value = '0'; // or anything you want to show here 
0

あなたはJavaScriptをisNaN()方法

{ 
     var payable_interest = (loan_amount * no_of_periods * rate_of_interest)/100; 

     var pay_interest = payable_interest.toFixed(2); 

     document.getElementById("total_payable_interest").value = isNaN(pay_interest) ? 0 : pay_interest; 


     var repay_amount = (loan_amount/no_of_months).toFixed(2); 

     document.getElementById("repayment_amount").value = isNaN(repay_amount) ? 0 : repay_amount; 

     var total_payable_amount = (payable_interest + loan_amount).toFixed(2); 

     document.getElementById("total_payable_amount").value = isNaN(total_payable_amount) ? 0 : total_payable_amount; 


    } 
1

使用を使用して以下のようにコードを変更することができますisNaN()機能

例:

を使用して
var repay_amount = (loan_amount/no_of_months).toFixed(2); 

if(!isNaN(repay_amount)) //if is NaN don't set value 
    document.getElementById("repayment_amount").value = repay_amount; 
0

がお手伝いします:

function calculateInterest() { 
 

 
    var repay_period = parseInt(document.getElementById("emp_loan_repayment_method_id").value) ; 
 
    var loan_amount = parseInt(document.getElementById("maximum_loan_amount").value); 
 
    var rate_of_interest = parseInt(document.getElementById("rate_of_interest").value); 
 
    var no_of_months = parseInt(document.getElementById("repayment_periods").value); 
 
    var repay_month_amount = parseInt(document.getElementById("repayment_amount").value); 
 
    var no_of_periods = no_of_months/12; 
 
    
 
    { 
 
     var payable_interest = (loan_amount * no_of_periods * rate_of_interest)/100; 
 
    
 
     var pay_interest = payable_interest.toFixed(2); 
 
     pay_interest = isNaN(pay_interest) ? 0 : pay_interest; 
 
     document.getElementById("total_payable_interest").value = pay_interest; 
 

 

 
     var repay_amount = (loan_amount/no_of_months).toFixed(2); 
 
     repay_amount = isNaN(repay_amount) ? 0 : repay_amount; 
 
     document.getElementById("repayment_amount").value = repay_amount; 
 

 
     var total_payable_amount = (payable_interest + loan_amount).toFixed(2); 
 
     total_payable_amount = isNaN(total_payable_amount) ? 0 : total_payable_amount; 
 
     document.getElementById("total_payable_amount").value = total_payable_amount; 
 

 

 
    } 
 
}
emp_loan_repayment_method : <input id="emp_loan_repayment_method_id" value="100" /> 
 
<br> 
 
maximum_loan_amount : <input id="maximum_loan_amount" value ="50" /> 
 
<br> 
 
rate_of_interest : <input id="rate_of_interest" value="2" /> 
 
<br> 
 
repayment_periods : <input id="repayment_periods" value="3" /> 
 
<br> 
 
repayment_amount : <input id="repayment_amount" value="200" /> 
 

 
<br><br><br> 
 

 
total_payable_interest : <input id="total_payable_interest" /> 
 
<br><br> 
 
total_payable_amount : <input id="total_payable_amount" /> 
 
<br><br> 
 

 
<button onclick="calculateInterest()" >Calculate</button>

関連する問題