我试图让我的输入字段只接受整数并编写了一些 java 来帮助验证,但是每次我单击下一个输入以插入一个值时,它都会发出我的警报。我错过了什么吗?
<script type="text/javascript">
var cartAmount, subtotal, pstAmount, pstTax, gstTax, taxAmount, shipCharge, afterTax;
function validate() {
//Whole Numbers with Regexp
var rx = new RegExp(/\d+/);
if(document.getElementsByClassName("inputValue").value != rx || document.getElementsByClassName("inputValue").value == "") {
alert("Please use only whole quantities.");
return false;
}
}
function sumCart() {
//Adding Cart Values
cartAmount = (document.frmMain.item1.value * 3.99) + (document.frmMain.item2.value * 5.99);
//Finding Subtotals
document.frmMain.subtotalAmt.value = Math.round(cartAmount*100)/100;
subtotal = document.frmMain.subtotalAmt.value;
//Finding Taxes
pstTax = document.frmMain.taxAmount.value;
gstTax = .05;
pstAmount = Math.round((subtotal * pstTax)*100)/100;
gstAmount = Math.round((subtotal * gstTax)*100)/100;
taxAmount = parseFloat(pstAmount,10) + parseFloat(gstAmount,10);
document.getElementById("tax").innerHTML = "Tax in dollars is $" + Math.round(taxAmount * 100)/100;
//Finding Total
shipCharge = 0;
afterTax = parseFloat(subtotal) + parseFloat(taxAmount) + parseFloat(shipCharge);
document.frmMain.totalAmt.value = Math.round(afterTax*100)/100;
}
</script>