简体   繁体   English

使用JavaScript在ASP.NET中进行将来的日期验证

[英]Future date validation in asp.net using javascript

Below mentioned code is been used to validate a future date, but it's not working. 下面提到的代码已用于验证未来日期,但无法正常工作。 Any help is appreaciated. 任何帮助表示赞赏。 The textbox has readonly property set to false so i can change the values in the text box and change to the future date,for that condition i need to validate 文本框的readonly属性设置为false,因此我可以更改文本框中的值并更改为将来的日期,为此我需要验证

// In aspx //在aspx中

  <asp:TextBox ID="txtdob" runat="server" OnClientDateSelectionChanged="checkDate"></asp:TextBox>

  <asp:Button ID="Button2" runat="server" onclick="Button2_Click" Height="26px" Width="23px" Text=".." />   
<script type="text/javascript" >
function checkDate(sender, args) 
{
    if (sender._selectedDate > new Date())
    {
        alert("You cannot select future date!");
        sender._selectedDate = new Date();
        // set the date back to the current date
        sender._txtdob.set_Value(sender._selectedDate.format(sender._format))
    }
}
</script>
<asp:Calendar ID="Calendar1" runat="server" onselectionchanged="Calendar1_SelectionChanged" OnClientDateSelectionChanged="checkDate"
   style="top: 320px; left: 246px; position: absolute; height: 188px; width: 259px">
</asp:Calendar>

//In c# //在C#中

protected void Button2_Click(object sender, EventArgs e)
{
   Calendar1.Visible = true;
}
protected void Calendar1_SelectionChanged(object sender, EventArgs e)
{
   txtdob.Text = Calendar1.SelectedDate.ToShortDateString();
}

This solution is no validation per request, but it makes all dates not selectable 此解决方案不会针对每个请求进行验证,但是会使所有日期都无法选择

protected void Calendar1_DayRender(object sender, DayRenderEventArgs e)
{
    endDate = DateTime.Today;
    e.Day.IsSelectable = e.Day.Date <= endDate;
}

Note that DayRenderEventArgs is in the Namespace System.Web.UI.WebControls 请注意, DayRenderEventArgs位于命名空间System.Web.UI.WebControls

OnClientDateSelectionChanged is not a valid method for asp calender control. OnClientDateSelectionChanged不是用于ASP日历控件的有效方法。 It is a method used in ajax calender control. 这是用于ajax压延机控制的方法。 Check this link 检查此链接

http://forums.asp.net/t/1922747.aspx http://forums.asp.net/t/1922747.aspx

First download Ajax Tool Kit which is free and then in Toolbox Add a New Tab then Choose items and browse for Ajax Control ToolKit dll file . 首先下载免费的Ajax工具套件,然后在“ 工具箱”中添加一个新选项卡,然后选择项目并浏览以查找Ajax Control ToolKit dll文件。 The tool kit will added to the toolbox and then select a calendar extender from the ajax tool kit. 该工具套件将添加到工具箱,然后从ajax工具套件中选择一个日历扩展器。 Then in aspx page add this lines of code before the ContentPlaceHolder 然后在aspx页面中,在ContentPlaceHolder之前添加以下代码行

 <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>

Then inside Content Place Holder add the JavaScript. 然后在Content Place Holder中添加JavaScript。

 <script type="text/javascript">
    function checkDate(sender, args) 
            {
                if (sender._selectedDate > new Date()) 
                {
                    alert("You cannot select future date!");
                    sender._selectedDate = new Date();
                    // set the date back to the current date
                    sender._textbox.set_Value(sender._selectedDate.format(sender._format))
                }
            }
</script>

And then for the textbox use this code. 然后对于文本框使用此代码。

   <asp:TextBox ID="txtdob" runat="server"  ></asp:TextBox>
         <asp:CalendarExtender runat="server" ID="CalenderExtender1" Format="MM/dd/yyyy" OnClientDateSelectionChanged="checkDate"
        TargetControlID="txtdob">
    </asp:CalendarExtender>

This might solve your issue. 这可能会解决您的问题。

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

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