Help with clent script in a MRVS to enforce a time selection between start and end times

Brett25
Tera Contributor

Hello, 

I really need help for the forum Guru's. I am in the process of creating a booking catalog request form. The request consists of a single day, multiple days, and long term booking options. For the single day, I just used the form variable of Date, and select boxes for start time and end time. I was able to create client scripts that prevented a user from entering a start time after the end time, and a end time before a start time. Both onchange of the start time and end time variables. To prevent submission I added a on submit script. They work great.  But when I applied the same logic to a MRVS it did not work.  The multiple days.short time mvrs also has a date field and a start time and end time. I tried to use the exact same client script for the onchange but every time the user filling out the form could choose a start time after the end time and a end time before the start time. I was told by another developer that this is common for MRVS especially when trying to validate an expression. 

It that true? If you can help or you can validate that what I want to do cant be dome, please let me know.

 

I included the start time script below:

 

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading) return;

    var startTime = newValue;
    var endTime = g_form.getValue('end_time');

    if (!startTime || !endTime || startTime === "Select time" || endTime === "Select time") return;

    function timeToMinutes(t) {
        var parts = t.split(/[: ]/);
        var hours = parseInt(parts[0], 10);
        var minutes = parseInt(parts[1], 10);
        var ampm = parts[2];

        if (ampm === 'PM' && hours !== 12) hours += 12;
        if (ampm === 'AM' && hours === 12) hours = 0;

        return hours * 60 + minutes;
    }

    if (timeToMinutes(startTime) >= timeToMinutes(endTime)) {
        g_form.addErrorMessage("Start Time must be earlier than End Time.");
        g_form.setValue('start_time', ''); // clear invalid value
    }
}
1 REPLY 1

Huynh Loc
Mega Sage

Hi @Brett25 ,

I don’t think there is an issue with your client script specifically related to MRVS.

Could you please share more details about the problem and include some screenshots? That would make it easier to review and verify the issue.