Date field allowing form to submit with disabled dates [RESOLVED]

Hey! I’m using a date picker for a couple of fields on my forms, and I set up a date range for them using:

function setDateRange(w,x,y,z){
       gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {
          if ( formId == w && fieldId == x ) {
              var ranges = [
                  { start: new Date(y), end: new Date(z) }
              optionsObj.beforeShowDay = function(date) {
                  for ( var i=0; i<ranges.length; i++ ) {
                      if ( date >= ranges[i].start && date <= ranges[i].end ) return [true, ''];
                  return [false, ''];
              optionsObj.minDate = ranges[0].start;
              optionsObj.maxDate = ranges[ranges.length -1].end;
          return optionsObj;

    setDateRange(1, 3, '03/31/1956', '01/01/2020'); // form id, field id, start date, end date
    setDateRange(2, 9, '03/31/1956', '01/01/2020');

This is working as far as the dates are unavailable outside of this range on the calendar that pops up, BUT if the user types in a date outside of this range, the form will allow them submit without throwing an error. What should I be doing to validate this field to make sure only dates within the given range are submitted?

Thanks in advance! :slight_smile:

Hi Brittany, the gform_datepicker_options_pre_init only targets the datepicker script to modify the options available on the calendar, it doesn’t perform any validation of the data.

If you want to validate the date submitted, you will want to use also the following validation filter to run your own custom validation PHP code: gform_field_validation - Gravity Forms Documentation

1 Like

This is perfect. Thank you! :slight_smile:

1 Like