Thursday 22 July, 2010

Validate if date is between two dates

I had this unique request coming from a user, he was using a Sharepoint list which had two date fields. Let us asume these fields to be "Order Date" and "Processed Date". The user wanted to make sure people enter a valid "Processed Date" which lied between "Order Date" and "Todays Date". Now SharePoint by default does not any such field validation functionality.

I tried googling, but there was nothing readily available for SharePoint. The next thing came to my mind was using a Javascript snippet, but how do I add the snippet to the code was another challenge. Please read through the article below to understand how to go about this.

1. First task was to create a list for the customer which had "Order Date" and "Processed Date"
2. Once this is created I need to write a Javascript snippet which will make sure the Processed Date is in between "Order Date" and todays date(current date)

so here is it:



3. Now that the snippet is ready I need to find a way to include this to the "NewForm.aspx" and also to "EditForm.aspx", Initially I though of using SharePoint Designer to do this, but somehow though there could be an easier way. Finally after 3 days and sevral cups of tea I found that I can add a content editor webpart the "NewForm.aspx" and "Editform.aspx" page . Normally "Edit Page" option is grayed out for these pages. But there is a cheat to get in

Just append "ToolPaneView=2" to the end of the URL for example

http://servername/site/sitename/lists/OrdProc/NewForm.aspx?ToolPaneView=2








4.Next step is to add a contet editor webpart to the bottom of the page and copy the javascript snippet to the source editor. Click on Save and Apply ok on the webpart properties menu. Thats it its done!!.


5. Now it is time for testing, first I tried to key in a "Processed date" before the "Order Date" and as you can see I got a pop up "The Processed Date should be between Order Date and Todays Date" Bingo!!


6. The second thing I need to test what happens if I choose a date later that todays date.





and as you can see in the above pictures, it worked!! . You can also use the same two validate date between two differrent dates other than today, just tweak the javascript.

3 comments:

Anonymous said...

Genial fill someone in on and this post helped me alot in my college assignement. Say thank you you as your information.

shekar said...

can u send the javascript code

Anonymous said...

http://linguinauts.com/node/46149