SharePoint Date Time Control Important Tips!


In this article i will explain some of the main tips that commonly used with SharePoint date time control.

Set SharePoint Date Time Control Validation:

Most of the cases you will need to set a validation to this control, although it has “IsRequired” property to maintain OTB validation but some times you would need to change this message or use “RequiredFieldValidator” control for some reason.

Assume that SharePoint date time control ID is “dtStart“, using  “RequiredFieldValidator” control with it should be in the following format:

ControlToValidate = [SharePointDateTimeControlID]$[SharePointDateTimeControlID]Date

<asp:RequiredFieldValidator id="RequiredFieldValidator1"
                 ControlToValidate="dtStart$dtStartDate" 
                 Display="Dynamic" runat=server
                 ErrorMessage="Date is Required"> 

 

Disable any Past Dates Values

If you want to prevent user from selecting any past dates less than Today’s date you have to set the following property:

In page load event

 dtStart.MinDate = DateTime.Today;

 
Prevent User from Typing or Changing Selected Date Value

The most important tip is that if you prevent user from selecting past dates, then he can type it or even type some text in this control text box!!

This control is consist of a text box plus date picker button

Date Time

To prevent user from doing this action you can easily make this text box “Read Only” !

On page load event type the following line of code to mark this text box as “Read Only”

((TextBox)(dtStart.Controls[0])).Attributes.Add("readonly", "readonly");

 

Then at the end you have a date time control with validation and you can make sure that selected date value is valid!

Enjoy this bite.