Text Box Should Accepts only Date using Calendar Extender

Sometimes we need validation for date, that user can only put the date in textbox.

Here is a method through which user can only select the date from calendar extender. User can't type, paste etc. in textbox.

Below is my TextBox along with Calendar Extender.

  1. <asp:TextBox ID="txtDate" runat="server" CssClass="mytextbox" Enabled="False"></asp:TextBox>    
  2.          <asp:CalendarExtender ID="txtDate_CalendarExtender" runat="server"     
  3.            Enabled="True" TargetControlID="txtDate" Format="dd/MM/yyyy">    
  4.          </asp:CalendarExtender>    
On the Page_Load event of this form you just need to add a attribute to the TextBox.
  1. protected void Page_Load(object sender, EventArgs e)    
  2.    {    
  3.         if (!IsPostBack)    
  4.         {    
  5.             txtDate.Attributes.Add("readonly""readonly");    
  6.         }    
  7.    }    
Now you can only select date from calendar extender. You can't type, paste etc. in textbox.

Ebook Download
View all
Learn
View all