2016-08-19 80 views
0

這是我用來發布數據的表單。所有這些在輸入方面都很好。這只是參考我的JavaScript驗證表單。如何添加日期驗證?

<form action= "<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" name="input" id="input"> 
      <br> 
      <table> 
       <tr> 
       <th>Animal Name</th> 
       <td><input type="text" name="ANM_NAME" id="ANM_NAME" maxlength="20"></td> 
       </tr> 
       <tr> 
       <th>Owner Name</th> 
       <td><input type="text" name="OWN_NAME" id="OWN_NAME" maxlength="20"></td> 
       </tr> 
       <tr> 
       <th>Sign in</th> 
       <td><input type="date" name="SIGN_IN" id="SIGN_IN"></td> 
       </tr> 
       <tr> 
       <th>Medication</th> 
       <td><input type="text" name="MEDS" id="MEDS" maxlength="20"></td> 
       </tr> 
       <tr> 
       <th>Special Requirements</th> 
       <td><input type="text" name="SPEC" id="SPEC" maxlength="20"></td> 
       </tr> 
       <tr> 
       <th>Food</th> 
       <td><input type="text" name="FOOD" id="FOOD" maxlength="20"></td> 
       </tr> 
       <tr> 
       <th>Notes</th> 
       <td><input type="text" name="NOTE" id="NOTE" maxlength="20"></td> 
       </tr> 
      </table> 
      <br> 
     <input type="button" id="button" value="Submit"> 
     </form> 

目前我有此進行驗證,點擊按鈕就設置了var標誌設置爲true,然後檢查輸入類型的文本,看看他們是否有文字,如果他們這樣做提交,如果不是它創建彈出提醒警報。

<!-- Checking if inputs are entered--> 
    <script> 
    $("#button").click(function(){ 
    var flag=true; 

    // check all input values 
    $("input[type='text']").each(function(){ 
     if($(this).val()==""){ 
      flag=false; 
     } 
    }); 

    // submit the form 
    if(flag){ 
     $("form#input").submit() 
    }else{ 
     // You should do something here. 
     alert("Please fill all inputs!"); 
    } 
    }); 
    </script> 

但它不會爲日期,因爲它是使用不同的輸入類型的工作。我如何將日期輸入添加到此驗證?

回答

1

您可以更然後使用一個分隔符一種類型的輸入的選擇:

<script> 
    $("#button").click(function() { 
    var flag = true; 

    // check all input values 
    $("input[type='text'],input[type='date']").each(function() { 
     if ($(this).val() == "") { 
     flag = false; 
     } 
    }); 

    // submit the form 
    if (flag) { 
     $("form#input").submit() 
    } else { 
     // You should do something here. 
     alert("Please fill all inputs!"); 
    } 
    }); 
</script>