javascript 如果输入日期大于今天,则抛出错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26701482/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
throw an error if the input date is greater than today
提问by Prajith A S
I am having an input date field in my php/html page. In my date field i need to throw an error if the input date is greater than today (sysdate). Is it possible? if so please help. Here is my html script of input field. Another JavaScript is also working based on the input date to the field to identify the due date automatically.
我的 php/html 页面中有一个输入日期字段。在我的日期字段中,如果输入日期大于今天(系统日期),我需要抛出错误。是否可以?如果是这样,请帮忙。这是我的输入字段的 html 脚本。另一个 JavaScript 也在根据字段的输入日期自动识别截止日期。
<div class="form-group">
<div class="input-group col-sm-4">
<div class="input-group-addon">Transaction Date <i class="fa fa-calendar"></i></div>
<input id ="txndt" class="form-control" type="date" name ="purch_date" onblur = "txn40();" required />
</div>
</div>
回答by Prajith A S
It worked with java script also. I have called a java script from my 'form'. Here is the solution.It is working as expected by me.
它也适用于 java 脚本。我从我的“表单”中调用了一个 java 脚本。这是解决方案。它按我的预期工作。
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 <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.
感谢大家的支持。
回答by Scimonster
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!")
}
};
回答by Prajith A S
Worked with 'max' function and 'php'. Working fine in chorome browser (HTML 5 supported browser). Changed the input with the below one.
使用“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 />