2014-11-02 46 views
-1

我在我的php/html頁面中有一個輸入日期字段。在我的日期字段中,如果輸入日期大於今天(sysdate),我需要拋出一個錯誤。可能嗎?如果是這樣,請幫助。 這是我的輸入字段的html腳本。另一個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> 
+0

您使用的是日曆庫嗎? – jfriend00 2014-11-02 16:21:03

+0

你應該發佈你的JavaScript。此外'日期'不是有效的輸入類型 – DarkBee 2014-11-02 16:21:12

+3

@DarkBee [與HTML5一起獲取](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/Input#Attributes)。 – Scimonster 2014-11-02 16:22:15

回答

2

它也適用於java腳本。我從我的'表單'中調用了一個java腳本。這是解決方案。它按我的預期工作。

形式

<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 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> 

感謝所有的支持。

0

您可以將valueAsDate屬性與當前日期進行比較。

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

我試圖限制這在輸入電平本身與最大功能。 (type =「date」max =「2014-11-02」)。有沒有可能將max設置爲sysdate? – 2014-11-02 16:40:53

+0

我不確定'max'與'type =「date」'一起工作。這基本上將最大值設置爲當前日期,如果您在上面進行更改,則會發出錯誤。如果你願意,你可以改變'alert'到'return false'來停止改變值。 – Scimonster 2014-11-02 16:42:36

+0

是的,它的最大工作。我做了這個:type =「date」max =「<?php echo date(」Y-m-d「)?>」。它按預期工作。 – 2014-11-02 16:52:31

0

使用'max'函數和'php'。在chorome瀏覽器(支持HTML 5的瀏覽器)中工作良好。用下面的輸入改變了輸入。

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