SharePoint Forms date validation using JavaScript

Out of the box SharePoint comes with a date-picker control you can use with Calendar, Tasks or any other items that use a date/time column. Unfortunately, the control is not smart enough to detect when a user creates an items with the End Date earlier than the Start Date. Sharepoint will accept such an entry without a blink.

You can prevent this from happening by adding a JavaScript script to the NewForm.aspx page. Edin Kapić has come with a solution. The script posted, however, uses a getTagFromIdentifierAndTitle function written by Edit and posted on another page. Some users may find it confusing or be unable to “compile” the two into a single script. For those who prefer to simply copy / paste the code and use it straight away here it goes:

<script language=”javascript” type=”text/javascript”>
function PreSaveAction()
{
var date1 = getTagFromIdentifierAndTitle(“INPUT”,”DateTimeFieldDate”,”Start Date”);
var date2 = getTagFromIdentifierAndTitle(“INPUT”,”DateTimeFieldDate”,”End Date”);
var arrDate1 = date1.value.split(“/”);
var useDate1 = new Date(arrDate1[2], arrDate1[1]-1, arrDate1[0]);
var arrDate2 = date2.value.split(“/”);
var useDate2 = new Date(arrDate2[2], arrDate2[1]-1, arrDate2[0]);
if(useDate1 > useDate2)
{
alert(“The End Date cannot happen earlier than the Start Date”);
return false; // Cancel the item save process
}
return true; // OK to proceed with the save item
}
function getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == “” || tempString.indexOf(identifier) == tempString.length – len)) {
return tags[i];
}
}
return null;
}
</script>

 

Once the script has been added to the New Item page users adding an item with incorrect dates will see the below when trying to submit the item:

post3

Perfect !!

5 Responses

  1. Hi

    I have sharepoint designer but dont know where to modify the code.
    I dont see and form.js file? Could you please help?

    • The easiest way to add JavaScript to your pages is using Content Editor WebPart (CEWP) – much easier and you don’t need to mess with the source code of the page itself. Do some googling on JavaScript and CEWP and you’ll find tons of stuff you probably didn’t know you can do with Sharepoint.

  2. Hi,

    If the start date & end date included the time. how to control the time validation using javascript?

    thx

  3. Hi Greg,

    Great post, It would be great if you could please tell me how to and where to modify this code using SharePoint Designer.

    I have done Google search as you suggested, but no luck there are tons of stuff directing to others tons of stuff.

    I would really appreciate if you could directly post the steps to modify it in SharePoint Designer.

    Thanks in advance..

  4. [...] other bits and pieces of this over time as well.)  Greg Osimowicz explains this in a post entitled SharePoint Forms date validation using JavaScript with reference to Edin Kapić’s post entitled Add JavaScript Date Validation into List Item [...]

Leave a Reply