Catalog Client Script - Validate the selected date that is either 1st or 16th day of the month

bbf35621
Kilo Sage

I have a client asking to validate a proposed date field in catalog form that is either the 1st or 16th day of a future month. For example, in onSubmit script, if I submit a form today, the next acceptable proposed date will be April 1st or 16th or May, June, July, or later on. How can I validate if the user picks either the 1st or 16th day of the future month? I am not very good at script calculating on the date. Can anyone help me with this, or do you have a similar code sample?

1 ACCEPTED SOLUTION

Maddysunil
Kilo Sage

@bbf35621 

Please try below code in onsubmit client script, just replace correct field names in code:

 

function onSubmit() {
    // Get the proposed date from the form
    var proposedDate = g_form.getValue('proposed_date_field'); // Replace 'proposed_date_field' with the actual field name of the proposed date
    
    // Parse the proposed date to a JavaScript Date object
    var proposedDateObj = new Date(proposedDate);
    
    var currentDate = new Date();
   
    // Check if the proposed date is in the future
    if (proposedDateObj > currentDate) {
        // Check if the proposed date is the 1st or 16th day of the month
        var dayOfMonth = proposedDateObj.getDate();
        if (dayOfMonth === 1 || dayOfMonth === 16) {
            // Proceed with form submission
            return true;
        } else {
            // Display error message and prevent form submission
            alert('Please select either the 1st or 16th day of a future month.');
            return false;
        }
    } else {
        // Display error message and prevent form submission
        alert('Please select a future date.');
        return false;
    }
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks

View solution in original post

6 REPLIES 6

Harish KM
Kilo Patron
Kilo Patron

Hi @bbf35621 the below code works for me

function onSubmit() {
var futureDate = g_form.getValue('date'); // date is the variable name

var selectedDate = new Date(futureDate);
// Check if the selected date is either the 1st or 16th day of the month
var dayOfMonth = selectedDate.getDate();
if (dayOfMonth !== 1 && dayOfMonth !== 16) {
alert("Please select either the 1st or the 16th day of the month.");
return false; // Prevent form submission
}

return true; // Allow form submission
   
}
 
HarishKM_0-1710812768659.png

 

Regards
Harish

Maddysunil
Kilo Sage

@bbf35621 

Please try below code in onsubmit client script, just replace correct field names in code:

 

function onSubmit() {
    // Get the proposed date from the form
    var proposedDate = g_form.getValue('proposed_date_field'); // Replace 'proposed_date_field' with the actual field name of the proposed date
    
    // Parse the proposed date to a JavaScript Date object
    var proposedDateObj = new Date(proposedDate);
    
    var currentDate = new Date();
   
    // Check if the proposed date is in the future
    if (proposedDateObj > currentDate) {
        // Check if the proposed date is the 1st or 16th day of the month
        var dayOfMonth = proposedDateObj.getDate();
        if (dayOfMonth === 1 || dayOfMonth === 16) {
            // Proceed with form submission
            return true;
        } else {
            // Display error message and prevent form submission
            alert('Please select either the 1st or 16th day of a future month.');
            return false;
        }
    } else {
        // Display error message and prevent form submission
        alert('Please select a future date.');
        return false;
    }
}

 

Please Mark Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.

 

Thanks