{"id":590,"date":"2015-06-30T22:49:06","date_gmt":"2015-06-30T10:49:06","guid":{"rendered":"http:\/\/geektactics.geektamin.com\/?p=590"},"modified":"2024-06-14T09:52:37","modified_gmt":"2024-06-13T21:52:37","slug":"gravity-forms-how-to-restrict-a-datepicker-date-range","status":"publish","type":"post","link":"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/","title":{"rendered":"Gravity Forms: How to Restrict a DatePicker Date Range"},"content":{"rendered":"
By default the Gravity Forms date picker has a date range of -100 years to +20 years.<\/p>\n
Sometimes you might need to limit that\u00a0date range.<\/p>\n
For example, a Date of Birth field can never be after today’s date. \u00a0And a requested appointment date\u00a0can’t be in the past.<\/p>\n
There is some easy code to restrict the date range shown in the date picker.<\/p>\n
Note: \u00a0The article applies to using a datepicker<\/strong>, not a date drop down date field in Gravity Forms.<\/p>\n <\/p>\n In this example the minimum date is set as 10 years ago: \u00a0-10 Y<\/p>\n The maximum date is set to today: 0<\/p>\n To limit the dates to only be today or the next two weeks, the code would be this:<\/p>\n Days are represented without a letter. \u00a0Months use the letter ‘M’, as shown in this example:<\/p>\n [gravityform id=”1″ title=”true” description=”false”]<\/p>\n So that is how to restrict the range of dates available in the date picker easily in Gravity Forms.<\/p>\n There are many more complex ways to use this\u00a0gform_datepicker_options_pre_init<\/strong> filter.\u00a0For complex examples see the Gravity Forms documentation page<\/a>.<\/p>\n If\u00a0you have found this article useful, please leave a comment below.<\/p>\n","protected":false},"excerpt":{"rendered":" By default the Gravity Forms date picker has a date range of -100 years to +20 years. Sometimes you might need to limit that\u00a0date range. For example, a Date of Birth field can never be after today’s date. \u00a0And a requested appointment date\u00a0can’t be in the past. There is some easy code to restrict the […]<\/p>\n","protected":false},"author":5,"featured_media":591,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[149],"class_list":["post-590","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-wordpress","tag-gravity-forms"],"yoast_head":"\nSteps to Limit the Date Range<\/h2>\n
\n
\ne.g. in this example the ID is “input_4_46”.
\nThis tells us that the form ID is 4 and the field ID is 46.<\/li>\nThe Code<\/h2>\n
Maximum Date = Today. \u00a0Minimum Date = 10 years ago<\/h3>\n
\r\n<script type=\"text\/javascript\">\r\n\r\ngform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {\r\n\r\nif ( formId == 1 && fieldId == 2 ) {\r\noptionsObj.minDate = '-10 Y';\r\noptionsObj.maxDate = 0;\r\n\r\n}\r\nreturn optionsObj;\r\n} );\r\n\r\n<\/script>\r\n <\/code><\/pre>\n
Dates Must Be in the Next Two Weeks<\/h3>\n
\r\n<script type=\"text\/javascript\">\r\n\r\n gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {\r\n\r\n if ( formId == 1 && fieldId == 3 ) {\r\n optionsObj.minDate = 0;\r\n optionsObj.maxDate = '+2 W';\r\n \r\n }\r\n return optionsObj;\r\n } );\r\n\r\n<\/script>\r\n\r\n<\/code><\/pre>\n
Date range from 10 Days Ago to 1 month away<\/h3>\n
\r\n<script type=\"text\/javascript\">\r\n\r\n gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {\r\n\r\n if ( formId == 1 && fieldId == 4 ) {\r\n optionsObj.minDate = '-10';\r\n optionsObj.maxDate = '+1 M';\r\n \r\n }\r\n return optionsObj;\r\n } );\r\n\r\n<\/script>\r\n\r\n<\/code><\/pre>\n
Live Demo<\/h2>\n