[英]How to Validate Date and Time with JavaScript
This code below is suppose to show an alert box if an incorrect date and/or time is selected. 下面的代码假定在选择了错误的日期和/或时间时显示一个警告框。
The problem I'm having is, If I selected 25/11/2015 and choose 11:00 It shows "Please change date or Time". 我遇到的问题是,如果我选择2015年11月25日并选择11:00,则会显示“请更改日期或时间”。 If I then select 25/11/2015 and choose 21:00 it shows "That's Fine".
如果然后选择2015年11月25日并选择21:00,则显示“那很好”。
The function is ignoring the date and just checking the time. 该功能忽略日期,仅检查时间。
It should show a message saying "Please change date or time" if the user selects a time that is gone already or a date that is before today. 如果用户选择已经过去的时间或今天之前的日期,则应显示一条消息“请更改日期或时间”。
I hope I can get help with this issue. 希望我能在这个问题上获得帮助。 Thanks in advance.
提前致谢。
function myFunction() { var today = new Date(); var select_text = document.getElementById("datepicker-leave").value; var select_date = new Date(select_text); var select_time = document.getElementById("leavedrop").value; var curr_time = today.getHours() + 1; if ((select_date.getTime() <= today.getTime()) && select_time < curr_time) { alert("Please change Date or Time") } else { alert("That's Fine") } } <script> $(function() { //datepickers $( "#datepicker-leave).datepicker({ dateFormat: 'dM-yy'}); }); </script>
<input type="text" id="datepicker-leave"> <select id="leavedrop"> <option value="0">00:00</option> <option value="1">01:00</option> <option value="2">02:00</option> <option value="3">03:00</option> <option value="4">04:00</option> <option value="5">05:00</option> <option value="6">06:00</option> <option value="7">07:00</option> <option value="8">08:00</option> <option value="9">09:00</option> <option value="10">10:00</option> <option value="11">11:00</option> <option value="12">12:00</option> <option value="13">13:00</option> <option value="14">14:00</option> <option value="15">15:00</option> <option value="16">16:00</option> <option value="17">17:00</option> <option value="18">18:00</option> <option value="19">19:00</option> <option value="20">20:00</option> <option value="21">21:00</option> <option value="22">22:00</option> <option value="24">23:00</option> </select> <input type="button" value="Submit" onClick="myFunction()" />
try the below structure for the date 尝试下面的日期结构
**$( "#date" ).datepicker({ dateFormat: 'dd/mm/yy' });**
Then try this 然后试试这个
$(function() {
$( "#date" ).datepicker({
dateFormat: 'd MM, yy',
onSelect: function(dateText, inst) {
var stop = dateText.indexOf(',');
alert( dateText.substring(0, stop));
}
});
}); });
In side on select you trigger the alert. 在侧面选择中,您将触发警报。
I hope this should help 我希望这会有所帮助
Thanks 谢谢
Its not working because you are not entering date in proper format 它不起作用,因为您没有以正确的格式输入日期
You need to use (YYYY-MM-DD)
format to make it work 您需要使用
(YYYY-MM-DD)
格式才能正常工作
for your example put date as 2015-11-25
对于您的示例,将日期设置为
2015-11-25
It seems like date object in JavaScript is pretty weak without any additions. 似乎JavaScript中的date对象很弱,没有任何添加。 I found the pretty useful library DateJS which gives you some powerful methods to solve your problem.
我发现了非常有用的库DateJS ,它为您提供了一些解决问题的强大方法。
Here is also a little guide from Datejs . 这也是Datejs的一些指南。
For example you could use the equals method: Date.equals(Date.today(), pickedDate());
例如,您可以使用equals方法:
Date.equals(Date.today(), pickedDate());
You have to read a little bit through the documentation to reach your goal. 您必须通读文档以达到目标。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.