fbpx

The form below has a date field, a time field, and a numerical field.

Using JavaScript, I’ve made a change in the time field trigger code to check if the time is less than 24 hours in the future. This can be used to add a premium to last-minute bookings, disable submission, whatever you need to have happen.

In the current example, a message is shown and the value in the numerical field is changed if the date/time selected is less than 24 hours in the future.

Repeaters With Titles

Repeaters With Titles

Hey Look It's a Repeater!


The code to accomplish this is below:

(function($){
    // Set variable to current date and time
    const now = new Date();
    let stamp = now.getTime();
    let tomorrow = stamp + (86400000);
    $('#field_time').change(function(){ 
        let date_array = $('#field_date').val().split('/');
        let time_array = $('#field_time').val().split(':');
        const time_slot  = new Date(date_array[2],(date_array[0] - 1),date_array[1],time_array[0],time_array[1]);
        let time_stamp = time_slot.getTime();
        if( time_stamp < tomorrow ) {
            $('#field_price').val(100);
            $('#field_notif').html('Your selected time is less than 24 hours in the future. Last-minute bookings are subject to a surcharge.');
        } else {
            $('#field_price').val(50);
            $('#field_notif').html('Your selected time provides adequate notice, so no surcharge will be levied.');
        }
    });
})(jQuery)