2017-04-12 58 views
0

我有一個銀行腳本。當用戶存款時,我想確保它是一個正整數。如果不是,我想把它們踢出去。如果聲明檢查負數

這裏是我的代碼:

<section id="pigBox"> 
     <img src="images/pig.png" /> 
     <label>Balance: </label><input type="text" id="balance" /> 
     <button id="deposit"> Deposit </button> 
     <button id="withdraw"> Withdraw </button> 
    </section><!-- end of pigBox--> 

document.getElementById('balance').value = "1000" 

var balance = document.getElementById('balance').value; 
var deposit = document.getElementById('deposit'); 
var withdraw = document.getElementById('withdraw'); 

deposit.addEventListener('click', depositCash); 
withdraw.addEventListener('click', withdrawCash); 

function depositCash() { 
    var depositAmt = prompt('How much would you like to deposit?'); 

    if(depositAmt != Number(depositAmt) && depositAmt) { 
    return alert('Please enter a valid integer.'); 

    } 
    balance = Number(balance) + Number(depositAmt); 
    document.getElementById('balance').value = balance; 
} 

function withdrawCash() { 
    var withdrawAmt = prompt('How much you you like to withdraw?'); 

    if(withdrawAmt != Number(withdrawAmt)) { 
    return alert('Please enter a valid integer.'); 

    } 
    balance = Number(balance) - Number(withdrawAmt); 
    document.getElementById('balance').value = balance; 
} 

我試着用..

else if(Number(depositAmt) < 0) { 
return alert('please enter a valid integer.'); 
} 

但是,這並不工作。我究竟做錯了什麼?

謝謝你們!

+1

'但是,這並不work'是什麼意思?你有錯誤嗎? – gurvinder372

+0

http://stackoverflow.com/questions/10834796/validate-that-a-string-is-a-positive-integer –

+0

可能重複的[驗證字符串是一個正整數](http://stackoverflow.com/questions/10834796/validate-that-a-string-is-a-positive-integer) –

回答

1

只是檢查

if (depositAmt <= 0) { 
    return alert('Please enter a positive integer.'); 
    } 

重寫你的if環路if - else if - else避免檢查所有條件。

if (depositAmt != Number(depositAmt) && depositAmt) { 
    return alert('Please enter a valid integer.'); 

    } else if (depositAmt <= 0) { 
    return alert('Please enter a positive integer.'); 

    } else { 
    balance = Number(balance) + Number(depositAmt); 
    document.getElementById('balance').value = balance; 
    } 

document.getElementById('balance').value = "1000" 
 
var balance, deposit, withdraw; 
 
balance = document.getElementById('balance').value; 
 
deposit = document.getElementById('deposit'); 
 
withdraw = document.getElementById('withdraw'); 
 

 
deposit.addEventListener('click', depositCash); 
 
withdraw.addEventListener('click', withdrawCash); 
 

 
function depositCash() { 
 
    var depositAmt; 
 
    depositAmt = prompt('How much would you like to deposit?'); 
 

 
    if (depositAmt != Number(depositAmt) && depositAmt) { 
 
    return alert('Please enter a valid integer.'); 
 

 
    } else if (depositAmt <= 0) { 
 
    return alert('Please enter a positive integer.'); 
 

 
    } else { 
 
    balance = Number(balance) + Number(depositAmt); 
 
    document.getElementById('balance').value = balance; 
 
    } 
 
} 
 

 
function withdrawCash() { 
 
    var withdrawAmt; 
 
    withdrawAmt = prompt('How much you you like to withdraw?'); 
 

 
    if (withdrawAmt != Number(withdrawAmt)) { 
 
    return alert('Please enter a valid integer.'); 
 

 
    } 
 
    balance = Number(balance) - Number(withdrawAmt); 
 
    document.getElementById('balance').value = balance; 
 
}
<section id="pigBox"> 
 
    <img src="images/pig.png" /> 
 
    <label>Balance: </label><input type="text" id="balance" /> 
 
    <button id="deposit"> Deposit </button> 
 
    <button id="withdraw"> Withdraw </button> 
 
</section> 
 
<!-- end of pigBox-->

1

檢查這樣的:

if(isNaN(Number(depositAmt)) || Number(depositAmt) < 0) { 
    return alert('please enter a valid integer.'); 
}