最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to set minimum and maximum date of asp:textbox textmode=date ??? using javascriptjquery - Stack Overflow

programmeradmin1浏览0评论

I have a asp:textbox , how can i set its minimum date to today using javascript:

With C# I am doing it like this and it works fine..but I have to do it using Js/Jquery

DateTime date = DateTime.Today.Date;
            String today = date.ToString("yyyy-MM-dd");

            tourStartDate.Attributes["min"] =today;
<asp:TextBox Width="95%" ID="tourStartDate" runat="server" TextMode="Date" onchange="SetDate()"></asp:TextBox></td>

I have a asp:textbox , how can i set its minimum date to today using javascript:

With C# I am doing it like this and it works fine..but I have to do it using Js/Jquery

DateTime date = DateTime.Today.Date;
            String today = date.ToString("yyyy-MM-dd");

            tourStartDate.Attributes["min"] =today;
<asp:TextBox Width="95%" ID="tourStartDate" runat="server" TextMode="Date" onchange="SetDate()"></asp:TextBox></td>
Share Improve this question asked Jun 29, 2015 at 5:30 Syed Muhammad YasirSyed Muhammad Yasir 1054 silver badges14 bronze badges 4
  • possible duplicate of Minimum and maximum date – Shirish Commented Jun 29, 2015 at 6:01
  • Can you show the SetDate() method ? – sudhansu63 Commented Jul 1, 2015 at 7:39
  • function SetDate() { if (document.getElementById('<%=tourEndDate.ClientID%>').value <= document.getElementById('<%=tourStartDate.ClientID%>').value) { $('#<%=tourEndDate.ClientID %>').val(document.getElementById('<%=tourStartDate.ClientID%>').value); } – Syed Muhammad Yasir Commented Jul 1, 2015 at 8:43
  • @SyedMuhammadYasir Please add the content of your ment to your question. After that delete your ment. – Reporter Commented Feb 15, 2017 at 13:30
Add a ment  | 

4 Answers 4

Reset to default 3

On Code Behind C#:

tourStartDate.Attributes["max"] = DateTime.Now.ToString("yyyy-MM-dd");

VB:

tourStartDate.Attributes("max") = Now.ToString("yyyy-MM-dd")

you need to put the ClientIdMode = static in the server control to get the static Id.

<asp:TextBox Width="95%" ID="tourStartDate" ClientIdMode = "static" runat="server" TextMode="Date" onchange="SetDate()"></asp:TextBox>

Jquery: Set today in attribute.

$("#tourStartDate").attr("min", (new Date()));

EDIT:

Can you try <input type="date" min="2015-07-01" max="2015-10-20">

JQuery:

  $("#tourStartDate").attr("min", (new Date()).toISOString().substring(0,10));

Make sure your doc type is html ( for HTML 5 controls)

EDIT2 :

JavaScript :

 document.getElementById('tourStartDate').setAttribute('min', (new Date()).toISOString().substring(0,10));

My design code is :

<asp:TextBox ID="txtDate" runat="server" MaxLength="30" CssClass="form-control" autoplete="off"></asp:TextBox>

Javascript code and jquery code

$(function () {
  $("#ContentPlaceHolder1_txtDate").datepicker({
    minDate: new Date(new Date().getTime() - 2 * 24 * 60 * 60 * 1000),
    maxDate: new Date()
  });
});

Although not a javascript/jQuery solution, this is another ASP.NET flavor. Use the OnInit to set the attributes.

ASPX:

 <asp:TextBox ID="calEnd" runat="server" CssClass="datefield" data-val="true" OnInit="cal_Init"  data-val-required="End date is required" type="date" />

Code Behind:

protected void cal_Init(object sender, EventArgs e)
    {
        var inp = (TextBox)sender;
        inp.Attributes.Add("min", (DateTime.Now.Year - 10).ToString() + "-01-01");
        inp.Attributes.Add("max", (DateTime.Now.Year + 2).ToString() + "-12-31");
    }
发布评论

评论列表(0)

  1. 暂无评论