2017-04-20 95 views
0

我重新調整了一些用於計算增值稅的代碼,但我需要驗證它不僅適用於空值,還適用於3,000,000以下的值。 Javascript並不是我的特長,所以我正在努力做一些看起來很基本的事情。到目前爲止,我有這樣的:窗體上的驗證

<script language="javascript"> 

    // starts calculation 
    function calculate() { 
     var vat, total, vatTotal, finalTotal; 

     var frm = document.calculator; 

     total = evalnum(frm.total.value); 
     if (total == "NaN") 
      total = 0; 
     if (total == 0) { 
      alert("Please enter a valid ammount first."); 
      frm.total.focus() 
      return 
     } 
     else 
      frm.total.value = evalpound(total); 
     { 
      vatTotal = total * 0.005; 
      frm.calcvat.value = evalpound(vatTotal); 
      frm.calctotal.value = evalpound(total + vatTotal); 
     } 
    } 

    //---------------------------------------------------------------------------- 
    // Scripts for VAT calculator 
    //---------------------------------------------------------------------------- 

    var frm = document.calculator; 

    // ---------------------------------------------------------------------------- 
    function selectBox(frmE, selectText) { 

     var i; 
     var selectIndex = 0; 

     for (var i = 0; i < document.calculator[frmE].length; i++) { 
      if (document.calculator[frmE].options[i].value == selectText) { 
       selectIndex = i; 
      } 
     } 

     return selectIndex; 
    } 

    // ---------------------------------------------------------------------------- 
    function evalnum(numstr) { 

     var i, c, neg; 
     var ret = ""; 
     var p = false; 

     neg = 1; 
     if (numstr.indexOf("-") >= 0) { 
      neg = -1; 
     } 

     for (i = numstr.length - 1; i >= 0; i--) { 
      c = numstr.charAt(i); 

      if (c == ".") { 
       if (p == false) 
        p = true; 
       else 
        c = ""; 
      } 

      if ((c < "0" || c > "9") && c != ".") 
       c = ""; 

      ret = c + ret; 
     } 

     if (ret == "" || ret == "NaN") 
      ret = "0"; 
     return parseFloat(ret * neg); 

    } 

    // ---------------------------------------------------------------------------- 
    function evalpound(num) { 

     var i, l, d; 
     var nums; 
     var ret; 

     nums = String(Math.round(num * 100)); 

     while (nums.length < 3) 
      nums = "0" + nums; 

     l = nums.length - 3; 
     ret = "." + nums.charAt(l + 1) + nums.charAt(l + 2); 

     d = 0; 
     for (i = l; i >= 0; i--) { 
      ret = nums.charAt(i) + ret; 
      d++; 
      if (d == 3 && i > 0) { 
       ret = "," + ret; 
       d = 0; 
      } 
     } 

     ret = "£" + ret; 
     return ret; 

    } 

</script> 

HTML代碼如下。

<form name="calculator"> 
<td class="calcbody" width="220" valign="top">Salary Bill:</td> 
<td class="calcbody" width="150"><input type="text" class="calcinput" name="total" title 
min="0" max format value size="9" style="width: 100px; text-align: right;" 
onchange="calculate(this)"></td> 
<tr> 
<td width="100" height="22" class="calcbody" align="center"><a class="calcbutton" 
href="javascript:calculate(this)">Calculate</a></td> 
<tr> 
<td colspan="2" class="calcsubtitle"><b>Results</b></td> 
<td></td> 
</tr> 
<tr> 
<td class="calcbody" valign="top">result</td> 
<td class="calcbody"><input type="text" class="calcinput" name="calcvat"></td> 
<td class="calcbody"></td> 
</tr> 
+0

添加html表單代碼也以識別Id –

+0

權利,對不起,不知道是否一切都需要!使用完整形式編輯 –

+0

始終使用相關代碼的[mcve]進行發佈。太少是無用的,但太多是壓倒性的。 – evolutionxbox

回答

0

我修好了。 HTML工程不管(這只是舊代碼)。我只是不稱職的改變的.js

 total = evalnum(frm.total.value); 
    if (total == "NaN") 
     total = 0; 
    if (total == 0) { 

成了

 total = evalnum(frm.total.value); 
    if (total == "NaN") 
     total = 0; 
    if (total < 3000000) { 

我一直試圖總== < 300萬之類的東西,而不是替換的==。正如我所說的一個非常非常基本的變化 - 感謝在任何情況下的幫助:)