简体   繁体   English

如果输入日期大于今天,则会引发错误

[英]throw an error if the input date is greater than today

I am having an input date field in my php/html page. 我在php / html页面中有一个输入日期字段。 In my date field i need to throw an error if the input date is greater than today (sysdate). 如果输入日期大于今天(sysdate),则在我的日期字段中需要抛出一个错误。 Is it possible? 可能吗? if so please help. 如果是这样,请帮助。 Here is my html script of input field. 这是我输入字段的html脚本。 Another JavaScript is also working based on the input date to the field to identify the due date automatically. 另一个JavaScript也在根据字段的输入日期来自动识别到期日期。

<div class="form-group">
<div class="input-group col-sm-4">
<div class="input-group-addon">Transaction Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i class="fa fa-calendar"></i></div>
<input id ="txndt" class="form-control" type="date" name ="purch_date" onblur = "txn40();" required />
</div>
</div>

It worked with java script also. 它也与Java脚本一起工作。 I have called a java script from my 'form'. 我从“表单”中调用了Java脚本。 Here is the solution.It is working as expected by me. 这是解决方案,它按我的预期工作。

Form 形成

<form  name="purchase" id ="purchase" method="post" action="" onsubmit="return checkform()">
    <div class="form-group">
        <div class="input-group col-sm-4">
            <div class="input-group-addon">Transaction Date&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<i class="fa fa-calendar"></i></div>
                <input id ="txndt" class="form-control" type="date" max="<?php echo date("Y-m-d")?>" name ="purch_date" value="<?php echo $query2['purch_date'] ?>" onblur = "txn40();" required />
        </div>
    </div>
</form>

Java Script Java脚本

<script type="text/javascript">
    function checkform()
    {
    var dateString = document.purchase.txndt.value;
    var myDate = new Date(dateString);
    var today = new Date();
         if (document.purchase.txndt.value == "")
          { 
          //something is wrong
          alert('REQUIRED FIELD ERROR: Please enter date in field!')
          return false;
          }
          else if (myDate>today)
          { 
          //something else is wrong
            alert('You cannot enter a date in the future!')
            return false;
          }
          // if script gets this far through all of your fields
          // without problems, it's ok and you can submit the form
          return true;
    }
</script>

Thanks to all for support. 感谢大家的支持。

You can compare the valueAsDate property against the current date. 您可以将valueAsDate属性与当前日期进行比较。

document.querySelector('#txndt').onchange = function() {
    if (this.valueAsDate > new Date) {
        alert("You can't pick a future date!")
    }
};

Worked with 'max' function and 'php'. 使用“最大”功能和“ PHP”。 Working fine in chorome browser (HTML 5 supported browser). 在chorome浏览器(支持HTML 5的浏览器)中工作正常。 Changed the input with the below one. 用以下内容更改了输入。

<input id ="txndt" class="form-control" type="date" max="<?php echo date("Y-m-d")?>" name ="purch_date" onblur = "txn40();" required />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM