简体   繁体   中英

Convert Datepicker to MM/DD/YYYY Format when submitting form

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM