我尝试在我的表单中添加type = "month“字段的验证,这样如果输入的日期不晚于当前日期,它就会发出警告。
到目前为止,iv设法使用onlcick将函数链接到我的提交按钮,iv获得了一个获取当前日期并将其存储在另一个变量中的变量,iv尝试编写if/else语句,但我认为逻辑是不正确的。
我将只展示适用于我的问题的代码,如果需要更多的代码,我可以提供。
我的表单:
    <div id="container">
    <form method="post" action="www.someplace.php" id="MovieForm">
         <div id="col-50">
            <label for="expmonth">Expiry Date</label>
            <input type="month" id="expmonth" name="expmonth">
                   </div>
<div id="col-50">
        <button type="submit"  onclick="return ValidateForm();"  id="btn"><Strong>Proceed to checkout</Strong> </button>
          </div>
      </form>我的JS函数:
<script>
function ValidateForm() {
  var expValidate = document.getElementById("expmonth").value;
  var today = new Date();
  var date = today.getFullYear()+'-'+(today.getMonth()+1)
  var isValid = false;
    if (expValidate > today) {
        isValid = true;
        alert("correct month") 
    }
    else {
        alert("Wrong month")
        return false;
    }
}因此,如果我运行该代码并输入一个高于当前日期的日期,它会给我一个警告,告诉我“错误的月份”,然后返回到表单(即使它应该显示“正确的月份”,然后将我转到下一页)。
但如果我运行代码并输入比当前日期低一个月的时间,同样的事情也会发生,它仍然会给出消息“错误的月份”,然后返回到表单。
发布于 2019-10-01 19:39:27
尝尝这个。它应该工作,以匹配日期使用日期输入类型。
<input type="date" id="expmonth" name="expmonth">
function ValidateForm() {
    var date = new Date($('#expmonth').val());
    console.log(date);
    var today = new Date();
    console.log(today);
    if (date >= today) {
        isValid = true;
        alert("correct month") 
    } else {
        alert("Wrong month")
        return false;
    }
}https://stackoverflow.com/questions/58183956
复制相似问题