Date validation script

MariaVitorS
Tera Contributor

Hi guys, how are you? Can you help me?


I have a request to create a client script to allow the user to select in my date field (due_date_tax_collection_guide) only Tuesdays and Thursdays, but he can only choose Tuesdays and Thursdays 4 days ahead of the current day. But I can't get it to work properly, could you help me?

1 ACCEPTED SOLUTION

Hello @MariaVitorS 

Those error message is of previously selected date. I have added a g_form.clearMessages() to clear any previous error message exist.

Here is the updated code:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) {
        return;
    }
	g_form.clearMessages();
    validateDate();
}

function validateDate() {
    var dateValue = g_form.getValue('date');
    if (dateValue) {
        var selectedDate = new Date(dateValue); // Convert selected date to a JavaScript Date object
        var currentDate = new Date(); // Today's date

        // Add 4 days to the current date
        currentDate.setDate(currentDate.getDate() + 4);

        // Get day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
        var dayOfWeek = selectedDate.getDay();

        // Validate: must be >= 4 days from today and either Tuesday (2) or Thursday (4)
        if (selectedDate < currentDate || (dayOfWeek !== 2 && dayOfWeek !== 4)) {
            g_form.clearValue('date'); // Clear invalid date
            g_form.addErrorMessage('Please select a valid date. The date must be at least 4 days from today and fall on a Tuesday or Thursday.');
        }
    }
}

This clears away any previously existing message in the form and satisfy the requirement. I hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar

 

 

 

View solution in original post

7 REPLIES 7

Hello @MariaVitorS 

For 12th it will not show error as shown in screenshot. If the date is within next 4 days or if it is not Tuesday or Thursday then only error message will appear. Please make sure that you close the error message before selecting another date.

 

If this helped please hit like and mark it as an accepted solution.

 

Thank You 

Juhi Poddar 

@Juhi Poddar For me the message is being displayed even on the 12th, I just copied and pasted the code and changed the fields that were 'date' for my real field...

It must send the message on days other than Tuesdays and Thursdays, and if it doesn't respect the rule of 4 days after the day the date is inserted (today). In other words, only Tuesdays and Thursdays that are 4 days after today's date....

MariaVitorS_0-1733497794933.png

MariaVitorS_1-1733498021812.png

 

 

Hello @MariaVitorS 

Those error message is of previously selected date. I have added a g_form.clearMessages() to clear any previous error message exist.

Here is the updated code:

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || !newValue) {
        return;
    }
	g_form.clearMessages();
    validateDate();
}

function validateDate() {
    var dateValue = g_form.getValue('date');
    if (dateValue) {
        var selectedDate = new Date(dateValue); // Convert selected date to a JavaScript Date object
        var currentDate = new Date(); // Today's date

        // Add 4 days to the current date
        currentDate.setDate(currentDate.getDate() + 4);

        // Get day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
        var dayOfWeek = selectedDate.getDay();

        // Validate: must be >= 4 days from today and either Tuesday (2) or Thursday (4)
        if (selectedDate < currentDate || (dayOfWeek !== 2 && dayOfWeek !== 4)) {
            g_form.clearValue('date'); // Clear invalid date
            g_form.addErrorMessage('Please select a valid date. The date must be at least 4 days from today and fall on a Tuesday or Thursday.');
        }
    }
}

This clears away any previously existing message in the form and satisfy the requirement. I hope this helps!

 

"If you found my answer helpful, please like and mark it as an "accepted solution". It helps others find the solution more easily and supports the community!"

 

Thank You
Juhi Poddar