Table Of Contents
- Introduction
- Objective
- Process
Introduction
With the requirement of Custom Forms in SharePoint comes the need for custom fields to achieve many functionalities which SharePoint provides as OOTB features.
One of them is providing Date Picker control to the input field.
We can provide an input text field for the user to enter dates manually, but this practice is wrong and not user or developer friendly.
If we go with this way, then we will have to do so many validations on the field so that the user doesn’t enter the date in the wrong format or simply enter anything in the field and save the details.
To overcome this, we turn towards the jQuery or Bootstrap date picker control.
These JavaScript libraries help both the user (ease in using the Site) and the Developer (validations and Date Format) to maintain proper Date Fields and UI.
In the following article, I’ll explain how we can use the Date Picker control in the custom form and set validations on it.
Objective
To use jQuery date picker control on SharePoint custom form with validations.
Process
- Create a column with ‘Date and Time’ type in SharePoint List.
Fig - Date and Time type Column
- Include the following JavaScript references in your HTML file -
Fig - JavaScript references for Date Picker Control- <link href="..SiteAssets/datepicker.css" rel="stylesheet" type="text/css">
- <link href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.css" rel="stylesheet" type="text/css">
- <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" type="text/javascript"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js" type="text/javascript"></script>
- <script src="../SiteAssets/datepicker-1.10.js" type="text/javascript"></script>
- Create div elements for Date Picker field -
Fig - Field element on which we will initialize date picker control- <input type="text" id="_startDatePicker" readonly>
- <input type="text" id="_endDatePicker" readonly>
- Following is the view of the input fields,

Fig - Input fields for Date Picker
- We have put ‘readonly’ attribute on input tag so that User will not be able to type anything in the input field.
- Now, we move on to the JavaScript code.
- We will initialize the Date Picker on the page load so that when we click on the input field, Calendar control will show up to select the date.
Fig - Initializing input field with date picker control- $('#_startDatePicker').datepicker();
- $('#_endDatePicker').datepicker();
- After initialization is done, when we click on the input field, we will see the Calendar control box.

Fig - Date Picker control on the Input field
- Select on any date and that date will be displayed in the input field.

Fig - Date is showing in the input field after selection
- To set validation on Date Picker control, we will use ‘moment.min.js’ library which provides many functionalities to date picker.
- Following is the code snippet that we need to add to the date picker initialization code so that Date Picker dates before today’s date are disabled.
- $('#_startDatePicker').datepicker({minDate: 0});

Fig - Date earlier than today’s date is disabled in the date picker




Priyank ChauhanPosted May 9, 2019, 1:18 AM
To clear date picker value, just user $("#Id").val("").
Seema NagarajuPosted Oct 17, 2018, 1:10 AM
How to clear/reset value of datepicker? My requirement is to change date based on dropdown selection.