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.');
}
但是,這並不工作。我究竟做錯了什麼?
謝謝你們!
'但是,這並不work'是什麼意思?你有錯誤嗎? – gurvinder372
http://stackoverflow.com/questions/10834796/validate-that-a-string-is-a-positive-integer –
可能重複的[驗證字符串是一個正整數](http://stackoverflow.com/questions/10834796/validate-that-a-string-is-a-positive-integer) –