I need to limit the Date field in a SharePoint 2007 list so the users can enter date from today and up to year after. How can i do it?
3 Answers
If you want this to run server-side, you can use the built-in SharePoint DateTimeControl, which is explained here.
If you'd rather do this client side, and there are some very, very good reasons why you'd want to do this client-side (chiefly, that the DateTimeControl will make a server call every time you change a month or year), there are lots of premade JavaScript calendar datepickers already out there (I believe I used this one the last time I set something like this up for my clients.
From there, you can either validate directly through JavaScript or you can grab the text that appears in the textbox and validate it on the server side via ASP or C#. Since you said this is just one part of a form, going the latter route shouldn't be too big of an imposition for the end user, and it's how native SharePoint forms handle validation as well.
Add a validation formula to the column. While they are largely unchanged from SharePoint 2007 to SharePoint 2016, and just about any article will apply, here's one on 2007:
https://msdn.microsoft.com/en-us/library/office/bb862071(v=office.12).aspx
A formula for your example:
=AND(yourDateColumn>=TODAY(),yourDateColumn<=TODAY()+365)
You could use 365.25 to better deal with leap years.
To get to the same date (month and day) in a year use:
=AND(yourDateColumn>=TODAY(),yourDateColumn<=DATE(YEAR(yourDateColumn)+1,MONTH(yourDateColumn),DAY(yourDateColumn)))
Leap year dates like 2/29/2016 and 2/29/2020 can be a problem, so here's a little better solution:
If you want 2/29/2016 to be mapped to 3/1/2017 then:
=AND(
yourDateColumn>=TODAY(),
yourDateColumn<=DATE(YEAR(yourDateColumn)+1,MONTH(yourDateColumn),DAY(yourDateColumn))
)
If you want 2/29/2016 to be mapped to 2/28/2017 (last date of the same month in the next year) then:
=AND(
yourDateColumn>=TODAY(),
yourDateColumn<=DATE(YEAR(yourDateColumn)+1,MONTH(yourDateColumn),DAY(yourDateColumn)) -
IF(AND(MONTH(yourDateColumn)=2,DAY(yourDateColumn)=29),1,0)
)
If you want to change the number of years, just edit the "+1" in the formula.
If you want to test against the 1st day of the same month in the next year then:
=AND(
yourDateColumn>=TODAY(),
yourDateColumn<=DATE(YEAR(yourDateColumn)+1,MONTH(yourDateColumn),1)
)
If you want to test against the last day of the same month in the next year then: (leap year is properly dealt with)
=AND(
yourDateColumn>=TODAY(),
yourDateColumn<=DATE(YEAR(yourDateColumn)+1,MONTH(yourDateColumn)+1,1)-1
)