{"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

\"datepicker\"<\/p>\n

Steps to Limit the Date Range<\/h2>\n
    \n
  1. After you have inserted your Date field, insert\u00a0a HTML field somewhere on the form. \u00a0I prefer to put it right beside the date picker for easy reference. \u00a0Change the title of the HTML field to something descriptive to help you easily identify it – e.g. “Script\u00a0to restrict dates”. \u00a0Remember, this title doesn’t appear on the front end for users to see. \u00a0It’s just for you.<\/li>\n
  2. Identify the form ID and field number of the date picker. \u00a0The easiest way to do this is to preview (or view) the form. \u00a0Right click in the date picker field and choose “Inspect Element”. \u00a0Look for the ID of field.
    \ne.g. in this example the ID is “input_4_46”.
    \n\"datepicker_html_inputID\"This tells us that the form ID is 4 and the field ID is 46.<\/li>\n
  3. Copy the code below. \u00a0Firstly edit the Form ID and field ID to match the field of your date picker.<\/li>\n
  4. Edit the code as needed to suit your desired date range.<\/li>\n
  5. Paste your edited code into the HTML field on the form.<\/li>\n
  6. Update the form, and the date picker\u00a0will now be restricting the dates for you.<\/li>\n<\/ol>\n

    The Code<\/h2>\n

    Maximum Date = Today. \u00a0Minimum Date = 10 years ago<\/h3>\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

    \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

    To limit the dates to only be today or the next two weeks, the code would be this:<\/p>\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

    Days are represented without a letter. \u00a0Months use the letter ‘M’, as shown in this example:<\/p>\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

    [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":"\nGravity Forms: How to Restrict a DatePicker Date Range<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Gravity Forms: How to Restrict a DatePicker Date Range\" \/>\n<meta property=\"og:description\" content=\"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 […]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/\" \/>\n<meta property=\"og:site_name\" content=\"Geektactics\" \/>\n<meta property=\"article:published_time\" content=\"2015-06-30T10:49:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-06-13T21:52:37+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/06\/datepicker.png\" \/>\n\t<meta property=\"og:image:width\" content=\"555\" \/>\n\t<meta property=\"og:image:height\" content=\"307\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Josh Moore\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Josh Moore\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/\"},\"author\":{\"name\":\"Josh Moore\",\"@id\":\"https:\/\/geektactics.co.nz\/#\/schema\/person\/6bb496f93975e50731c7b169c39bc4ab\"},\"headline\":\"Gravity Forms: How to Restrict a DatePicker Date Range\",\"datePublished\":\"2015-06-30T10:49:06+00:00\",\"dateModified\":\"2024-06-13T21:52:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/\"},\"wordCount\":430,\"commentCount\":21,\"publisher\":{\"@id\":\"https:\/\/geektactics.co.nz\/#organization\"},\"image\":{\"@id\":\"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/06\/datepicker.png\",\"keywords\":[\"Gravity Forms\"],\"articleSection\":[\"Wordpress\"],\"inLanguage\":\"en-NZ\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/\",\"url\":\"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/\",\"name\":\"Gravity Forms: How to Restrict a DatePicker Date Range\",\"isPartOf\":{\"@id\":\"https:\/\/geektactics.co.nz\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/06\/datepicker.png\",\"datePublished\":\"2015-06-30T10:49:06+00:00\",\"dateModified\":\"2024-06-13T21:52:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/#breadcrumb\"},\"inLanguage\":\"en-NZ\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-NZ\",\"@id\":\"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/#primaryimage\",\"url\":\"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/06\/datepicker.png\",\"contentUrl\":\"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/06\/datepicker.png\",\"width\":555,\"height\":307},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/geektactics.co.nz\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Gravity Forms: How to Restrict a DatePicker Date Range\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/geektactics.co.nz\/#website\",\"url\":\"https:\/\/geektactics.co.nz\/\",\"name\":\"Geektactics\",\"description\":\"Interesting Stuff for Geeks\",\"publisher\":{\"@id\":\"https:\/\/geektactics.co.nz\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/geektactics.co.nz\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-NZ\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/geektactics.co.nz\/#organization\",\"name\":\"Geektactics\",\"url\":\"https:\/\/geektactics.co.nz\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-NZ\",\"@id\":\"https:\/\/geektactics.co.nz\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2024\/06\/geektactics-logo.png\",\"contentUrl\":\"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2024\/06\/geektactics-logo.png\",\"width\":2001,\"height\":831,\"caption\":\"Geektactics\"},\"image\":{\"@id\":\"https:\/\/geektactics.co.nz\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/geektactics.co.nz\/#\/schema\/person\/6bb496f93975e50731c7b169c39bc4ab\",\"name\":\"Josh Moore\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-NZ\",\"@id\":\"https:\/\/geektactics.co.nz\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/2bea43488633fc79ace340300f8ab6d6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/2bea43488633fc79ace340300f8ab6d6?s=96&d=mm&r=g\",\"caption\":\"Josh Moore\"},\"description\":\"I love marketing and technology. I run Duoplus Online Marketing and enjoy helping companies grow their income especially through using online marketing effectively.\",\"sameAs\":[\"https:\/\/www.duoplus.nz\"],\"url\":\"https:\/\/geektactics.co.nz\/blog\/author\/josh\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Gravity Forms: How to Restrict a DatePicker Date Range","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/","og_locale":"en_US","og_type":"article","og_title":"Gravity Forms: How to Restrict a DatePicker Date Range","og_description":"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 […]","og_url":"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/","og_site_name":"Geektactics","article_published_time":"2015-06-30T10:49:06+00:00","article_modified_time":"2024-06-13T21:52:37+00:00","og_image":[{"width":555,"height":307,"url":"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/06\/datepicker.png","type":"image\/png"}],"author":"Josh Moore","twitter_misc":{"Written by":"Josh Moore","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/#article","isPartOf":{"@id":"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/"},"author":{"name":"Josh Moore","@id":"https:\/\/geektactics.co.nz\/#\/schema\/person\/6bb496f93975e50731c7b169c39bc4ab"},"headline":"Gravity Forms: How to Restrict a DatePicker Date Range","datePublished":"2015-06-30T10:49:06+00:00","dateModified":"2024-06-13T21:52:37+00:00","mainEntityOfPage":{"@id":"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/"},"wordCount":430,"commentCount":21,"publisher":{"@id":"https:\/\/geektactics.co.nz\/#organization"},"image":{"@id":"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/#primaryimage"},"thumbnailUrl":"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/06\/datepicker.png","keywords":["Gravity Forms"],"articleSection":["Wordpress"],"inLanguage":"en-NZ","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/","url":"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/","name":"Gravity Forms: How to Restrict a DatePicker Date Range","isPartOf":{"@id":"https:\/\/geektactics.co.nz\/#website"},"primaryImageOfPage":{"@id":"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/#primaryimage"},"image":{"@id":"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/#primaryimage"},"thumbnailUrl":"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/06\/datepicker.png","datePublished":"2015-06-30T10:49:06+00:00","dateModified":"2024-06-13T21:52:37+00:00","breadcrumb":{"@id":"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/#breadcrumb"},"inLanguage":"en-NZ","potentialAction":[{"@type":"ReadAction","target":["https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/"]}]},{"@type":"ImageObject","inLanguage":"en-NZ","@id":"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/#primaryimage","url":"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/06\/datepicker.png","contentUrl":"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2015\/06\/datepicker.png","width":555,"height":307},{"@type":"BreadcrumbList","@id":"https:\/\/geektactics.co.nz\/blog\/gravity-forms-how-to-restrict-a-datepicker-date-range\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/geektactics.co.nz\/"},{"@type":"ListItem","position":2,"name":"Gravity Forms: How to Restrict a DatePicker Date Range"}]},{"@type":"WebSite","@id":"https:\/\/geektactics.co.nz\/#website","url":"https:\/\/geektactics.co.nz\/","name":"Geektactics","description":"Interesting Stuff for Geeks","publisher":{"@id":"https:\/\/geektactics.co.nz\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/geektactics.co.nz\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-NZ"},{"@type":"Organization","@id":"https:\/\/geektactics.co.nz\/#organization","name":"Geektactics","url":"https:\/\/geektactics.co.nz\/","logo":{"@type":"ImageObject","inLanguage":"en-NZ","@id":"https:\/\/geektactics.co.nz\/#\/schema\/logo\/image\/","url":"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2024\/06\/geektactics-logo.png","contentUrl":"https:\/\/geektactics.co.nz\/wp-content\/uploads\/sites\/2\/2024\/06\/geektactics-logo.png","width":2001,"height":831,"caption":"Geektactics"},"image":{"@id":"https:\/\/geektactics.co.nz\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/geektactics.co.nz\/#\/schema\/person\/6bb496f93975e50731c7b169c39bc4ab","name":"Josh Moore","image":{"@type":"ImageObject","inLanguage":"en-NZ","@id":"https:\/\/geektactics.co.nz\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/2bea43488633fc79ace340300f8ab6d6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/2bea43488633fc79ace340300f8ab6d6?s=96&d=mm&r=g","caption":"Josh Moore"},"description":"I love marketing and technology. I run Duoplus Online Marketing and enjoy helping companies grow their income especially through using online marketing effectively.","sameAs":["https:\/\/www.duoplus.nz"],"url":"https:\/\/geektactics.co.nz\/blog\/author\/josh\/"}]}},"_links":{"self":[{"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/posts\/590"}],"collection":[{"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/comments?post=590"}],"version-history":[{"count":6,"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/posts\/590\/revisions"}],"predecessor-version":[{"id":20280,"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/posts\/590\/revisions\/20280"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/media\/591"}],"wp:attachment":[{"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/media?parent=590"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/categories?post=590"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geektactics.co.nz\/wp-json\/wp\/v2\/tags?post=590"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}