我正在使用ajax calendarextender和javascript来限制用户不会选择过去的日期,但我在这里面临一个问题:
但如果用户选择当前日期,那么他也会收到不需要的警报。
我的要求是用户可以选择当前日期加上未来日期,但限制输入任何以前的日期。请帮我解决这个问题,下面你可以找到代码: -
<div id="formload" >
<!-- ============================== Fieldset 1 ============================== -->
<fieldset>
<legend>Enter Date</legend>
<table>
<tr>
<td><label for="input-one" class="float"><strong>Date</strong></label>
</td><br />
<td><asp:TextBox ID="txtDate" runat="server" CssClass="inp-text" Width="300px"></asp:TextBox> </td>
<td><asp:Image runat="server" ID="btnDate2" AlternateText="cal2" ImageUrl="~/App_Themes/Images/icon_calendar.jpg" />
<ajaxtoolkit:calendarextender runat="server" ID="calExtender2" Format="dddd, MMMM dd, yyyy" PopupButtonID="btnDate2" TargetControlID="txtDate" OnClientDateSelectionChanged="CheckDateEalier" /> </td>
</tr></table>
</fieldset>
<!-- ============================== Fieldset 1 end ============================== -->
的Javascript
<script type="text/javascript">
function CheckDateEalier(sender, args)
{
if (sender._selectedDate < new Date())
{
alert("Day earlier than today cannot be selected.");
sender._selectedDate = new Date();
sender._textbox.set_Value(sender._selectedDate.format(sender._format))
}
}
</script>
答案 0 :(得分:1)
这是因为日期相同,但时间不同,设置小时min sec和milli秒为0然后它将起作用。
<script type="text/javascript">
function CheckDateEalier(sender, args) {
var toDate = new Date();
var selectedDate = sender._selectedDate;
toDate.setMinutes(0);
toDate.setSeconds(0);
toDate.setHours(0);
toDate.setMilliseconds(0);
selectedDate.setMinutes(0);
selectedDate.setSeconds(0);
selectedDate.setHours(0);
selectedDate.setMilliseconds(0);
//alert(sender._selectedDate < toDate);
//var difference = sender._selectedDate - toDate;
if (selectedDate > toDate) {
alert("Ticket issue date must be greater than current date.");
sender._selectedDate = toDate; //set the date back to the current date
sender._textbox.set_Value(sender._selectedDate.format(sender._format));
}
}