2016-08-22 62 views
0

我有一個包含6個輸入框的表單。我想驗證用戶是否填寫了每個輸入的數字,並且數字總共爲100。我有一個函數可以驗證數字總數是否超過100,但是如何驗證數字小於100?現在,在輸入所有值之前,我會收到一條錯誤消息。確認輸入的所有輸入總計等於100

HTML

<fieldset>  
    <input type="text" name="qty" id="qty1" oninput="findTotal()" class="hvr-pop" required/>       
    <input type="text" name="qty" id="qty2" oninput="findTotal()" class="hvr-pop" required/>       
    <input type="text" name="qty" id="qty3" oninput="findTotal()" class="hvr-pop" required/>       
    <input type="text" name="qty" id="qty4" oninput="findTotal()" class="hvr-pop" required/>        
    <input type="text" name="qty" id="qty5" oninput="findTotal()" class="hvr-pop" required/>        
    <input type="text" name="qty" id="qty6" oninput="findTotal()" class="hvr-pop" required/>   
</fieldset>  
<output type="text" name="total" id="total" class="hvr-pop" readonly placeholder="Must total 100"></output> 

的JavaScript

function findTotal() { 
     "use strict"; 
     var arr = document.getElementsByName('qty'); 
     var tot = 0; 
     for(var i=0; i<arr.length;i++){ 
      if(parseInt(arr[i].value)) { 
       tot += parseInt(arr[i].value); 

       } 
      }      
     document.getElementById('total').value = tot; 
     // check that weightage scores = 100  
     if (tot > 100) { 
      alert("Please make sure numbers total 100."); 
       $("input").focus(function() { 
        $(this).css("border-color", "red").val(""); 
       }); 
       $('#form1').find('#total').val(''); 
       return false; 
      } 
    }  
+1

是否有一個原因,你爲什麼做驗證oninput'而不是使用按鈕來觸發它?如果每次有人輸入內容時檢查總數是否小於100,則每次輸入值時都會收到警報。 – APAD1

+0

如果它多於或少於100,你是否在做任何不同的事情?如果結果是相同的,只要它不等於100就可以使用!=或!==(取決於類型安全性) – Aaron

+0

如果6個輸入的總和小於或大於100,應該觸發警報。如果我使用!==而不是>,則在輸入所有輸入之前彈出警報。 – invincibleme

回答

0

正如@Aaron上面提到的,你可以簡單地將greater than標誌更改爲does not equal標誌,如果你要治療均小於或大於100同樣的方式:

<fieldset>  
 
    <input type="text" name="qty" id="qty1" oninput="findTotal()" class="hvr-pop" required/>       
 
    <input type="text" name="qty" id="qty2" oninput="findTotal()" class="hvr-pop" required/>       
 
    <input type="text" name="qty" id="qty3" oninput="findTotal()" class="hvr-pop" required/>       
 
    <input type="text" name="qty" id="qty4" oninput="findTotal()" class="hvr-pop" required/>        
 
    <input type="text" name="qty" id="qty5" oninput="findTotal()" class="hvr-pop" required/>        
 
    <input type="text" name="qty" id="qty6" oninput="findTotal()" class="hvr-pop" required/>   
 
</fieldset>  
 
<output type="text" name="total" id="total" class="hvr-pop" readonly placeholder="Must total 100"></output> 
 

 
<script> 
 
    function findTotal() { 
 
     "use strict"; 
 
     var arr = document.getElementsByName('qty'); 
 
     var tot = 0; 
 
     for(var i=0; i<arr.length;i++){ 
 
      if(parseInt(arr[i].value)) { 
 
       tot += parseInt(arr[i].value); 
 

 
       } 
 
      }      
 
     document.getElementById('total').value = tot; 
 
     // check that weightage scores = 100  
 
     if (tot !== 100) { 
 
      alert("Please make sure numbers total 100."); 
 
       $("input").focus(function() { 
 
        $(this).css("border-color", "red").val(""); 
 
       }); 
 
       $('#form1').find('#total').val(''); 
 
       return false; 
 
      } 
 
    } 
 
</script>

然而,正如我所說,這是一個不好的用戶體驗,因爲驗證正在發生的每有人進入一個值的輸入時間,這意味着他們將不得不解僱多個警報窗口正好填補了所有的輸入。相反,您應該點擊按鈕進行驗證,或者至少在驗證之前等待所有輸入都有值。

+0

是的,我同意。我想驗證的是在彈出警報之前輸入所有輸入。不知道如何做到這一點,如果有可能的話 – invincibleme

0

爲了達到預期的結果,下面添加如果條件

if (tot < 100 && arr[5].value) { 
    alert("Please make sure numbers total 100"); 
    for (i = 0; i < arr.length; i++) { 
     arr[i].value = null; 
    } 
    } 

HTML:

<fieldset>  
    <input type="text" name="qty" id="qty1" oninput="findTotal()" class="hvr-pop" required/>       
    <input type="text" name="qty" id="qty2" oninput="findTotal()" class="hvr-pop" required/>       
    <input type="text" name="qty" id="qty3" oninput="findTotal()" class="hvr-pop" required/>       
    <input type="text" name="qty" id="qty4" oninput="findTotal()" class="hvr-pop" required/>        
    <input type="text" name="qty" id="qty5" oninput="findTotal()" class="hvr-pop" required/>        
    <input type="text" name="qty" id="qty6" oninput="findTotal()" class="hvr-pop" required/>   
</fieldset>  
<output type="text" name="total" id="total" class="hvr-pop" readonly placeholder="Must total 100"></output> 

JS:

function findTotal() { 
     "use strict"; 
     var arr = document.getElementsByName('qty'); 
     var tot = 0; 
     for(var i=0; i<arr.length;i++){ 
      if(parseInt(arr[i].value)) { 
       tot += parseInt(arr[i].value); 

       } 
      }      
     document.getElementById('total').value = tot; 
     // check that weightage scores = 100  
     if (tot > 100) { 
      alert("Please make sure numbers total 100."); 
       $("input").focus(function() { 
        $(this).css("border-color", "red").val(""); 
       }); 
       $('#form1').find('#total').val(''); 
       return false; 
      } 

if (tot < 100 && arr[5].value) { 
    alert("Please make sure numbers total 100"); 
    for (i = 0; i < arr.length; i++) { 
     arr[i].value = null; 
    } 
    } 



    } 

http://codepen.io/nagasai/pen/yJWoLA

+0

@updated codepen其中總共工作少於100 ..希望這對你有用:) –

+0

感謝,娜迦賽A.這對於少於100的偉大作品,但現在如果它正好是100,我收到警報。 – invincibleme

+0

我沒有收到警報。可以給你輸入的樣本值,我試過10,20,30,30,5,5 –