2016-08-14 17 views
0

6つの入力ボックスがあり、100までの入力ボックスがあります.100にputを追加しない場合や、入力が空の場合、ボタンをクリックするとアラートが表示されます。これまでは、合計が正しくない場合は警告を受け取ることができますが、空欄の場合は通知を受け取ることはできません。フォーマルの妥当性確認

HTML

 <fieldset> 

      <input type="text" name="qty" oninput="findTotal()" id="qty1" class="hvr-pop" required/> 

      <input type="text" name="qty" oninput="findTotal()" id="qty2" class="hvr-pop" required/>  

      <input type="text" name="qty" oninput="findTotal()" id="qty3" class="hvr-pop" required/>  

      <input type="text" name="qty" oninput="findTotal()" id="qty4" class="hvr-pop" required/>  

      <input type="text" name="qty" oninput="findTotal()" id="qty5" class="hvr-pop" required/>  

      <input type="text" name="qty" oninput="findTotal()" id="qty6" class="hvr-pop" required/>  

     </fieldset>  

     <button type="button" class="weightageButton" onclick="validateTotal()">TOTAL WEIGHTAGE</button> 
     <output type="text" name="total" id="total" class="hvr-pop" readonly value="Must total 100"></output> 

Javascriptを

for(var i=0; i<arr.length;i++){ 
     if(parseInt(arr[i].value) === null || score > 100 || score < 100) { 

      alert("Please make sure each weightage is filled in and the total adds up to 100."); 
      $('#form1').find('input').val(''); 
      $('#form1').find('#total').val(''); 
      return false; 
     } 



    } 
} 

答えて

0

テキストフィールドは任意の文字値を持っている場合は、あなたが期待しているとして、その後parseInt(arr[i].value) === nullが、動作しません。
isNaN()関数は、値が不正な数値かどうかを判定します。 Number.isNan()は値をNumberに変換せず、Number型以外の値についてはtrueを返しません。 parseInt(arr[i].value) === nullの代わりにisNaN(parseInt(arr[i].value))を使用してください。

0

事は、空の文字列のためのparseInt()NaN、ないNULLを返すということです。あなたはこれに現在のチェックを交換する必要があります。

if (isNaN(parseInt(arr[i].value)) || ...) { ... } 
+0

完璧、ありがとう! – invincibleme

+0

あなたは大歓迎です:) –