Home > other >  Convert Datepicker to MM/DD/YYYY Format when submitting form
Convert Datepicker to MM/DD/YYYY Format when submitting form

Time:07-27

I have created a subscription order form on my website with Gravity Forms. The form includes a date field formatted with a Datepicker. I added custom Javascript to limit the viable selection of dates to anything a day away. That code is as follows:

$('#input_14_11').datepicker({
minDate : ' 1d'});

It serves its purpose and users are not able to select any past dates from the datepicker but a new issue has risen: The form cannot submit but instead returns an error message of "Please enter a valid date in the format (mm/dd/yyyy)." When I select a date it displays in the field the full date, example: (July 27,2022). However, when I manually enter a date in the specified format (07/27/2022), the form submits without an issue.

I essentially need to convert that selected "July 27,2022" into the correct format of 07/27/2022 when a user hits submit on the form. Example of returned error

CodePudding user response:

$( "#input_14_11" ).datepicker({ dateFormat: 'mm-dd-yy',minDate : ' 1d' });

CodePudding user response:

Instead of instantiating a new datepicker you could customize the one instantiated by Gravity Forms by using the gform_datepicker_options_pre_init filter e.g.

gform.addFilter( 'gform_datepicker_options_pre_init', function( optionsObj, formId, fieldId ) {
    if ( formId == 14 && fieldId == 11 ) {
        optionsObj.minDate = ' 1d';
    }
    return optionsObj;
});

By using this approach all the other datepicker properties, such as the date format, will remain the same.

  • Related