2017-04-20 7 views
0

VAT 처리를위한 코드를 다시 작성했지만 빈 값뿐만 아니라 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

ID가 –

+0

인 것을 확인할 수있는 html 폼 코드도 추가합니다. 미안하지만, 모든 것이 필요할 경우 확실하지 않았습니다! 전체 양식으로 편집 –

+0

항상 관련 코드의 [mcve]로 게시하십시오. 너무 적 으면 쓸모 없지만 너무 많은 것은 압도적입니다. – evolutionxbox

답변

0

수정했습니다. HTML은 관계없이 작동합니다 (단지 오래된 코드 일뿐입니다). 나는

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

내가 총 ==에게 < 3000000 물건을 시도하고, 대신 ==를 교체 유지

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

가 된의 .js 변경에 단지 적합하지이었다. 내가 아주 아주 기본적인 변화를 말했듯이 - 어떤 경우에도 도움을 주셔서 감사합니다 :)