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

Siddhesh Wani1
Tera Guru
Hi, Please refer this code and try some combinations and check this for the compare to function
 
var gdate = new GlideDateTime();
var proposedDate = g_form.getValue('u_proposed_date'); // u_proposed_date = your field name
gs.info("Current Date: "+proposedDate.getDate());
gs.info("Current Month: "+proposedDate.getMonthLocalTime());

var sixteenthday = firstday.addDays(15)
var firstday = gs.beginningOfThisMonth(proposedDate);


gs.info("first date of this month: "+firstDate);
gs.info("Last date of this month: "+endDate);

gs.info(proposedDate.compareTo(firstday));
gs.info(proposedDate.compareTo(sixteenthday));
// Equals (0)
// proposedDate is after sixteenthday (1)
 // proposedDate is before sixteenthday (-1)
 
Please Mark Correct if this solves your query and also mark Helpful if you find my response worthy based on the impact.

Sumanth16
Kilo Patron

Hi @bbf35621 ,

 

Use onChange() Client Script with GlideAjax on your date restrict the value in date field.

Try this in the background script.

var gd = new GlideDate();
gs.info(gd.getDayOfMonthNoTZ());

if(gd.getDayOfMonthNoTZ() > 1)
    gs.print('Only 1st date of month is allowed.');
else
    gs.print('Date is correct.');

For Scoped Application, see the below image for reference.

Sumanth16_0-1710793339181.png

 

For Global Scope Applicationsee the below image for reference.

Sumanth16_1-1710793339162.png

 

If I could help you with your Query then, please hit the Thumb Icon and mark it as Correct !!

 

Thanks & Regards,

Sumanth Meda

I tried your sample code and it didn't work. The gs.info printed, "The selected day is undefined". Here is my script include,

var dateId = this.getParameter('sysparm_date');
        gs.info('The date is ' + dateId);
        var gd = new GlideDateTime(dateId);

        gs.info('The selected day is ' + gd.getDayOfMonthNoTZ());

        if(gd.getDayOfMonthNoTZ() == 1 || gd.getDayOfMonthNoTZ() == 16){
            gs.info('That is CORRECT!');
        }else{
            gs.info('User selected the wrong date');
        }

JohnnySnow
Kilo Sage

Hi @bbf35621 ,

 

Can you try this:

 

var today = gs.localtime();

var today = gs.localtime();
var currentMonth = today.getMonth() + 1; 
var currentYear = today.getFullYear();
var proposedDate = current_variable.proposed_date; // Replace 'current_variable' with your actual variable name

// Check if proposed date is in the future month
if (proposedDate.getMonth() !== currentMonth) {
    // Check if proposed day is 1st or 16th
    if (proposedDate.getDate() === 1 || proposedDate.getDate() === 16) {
        // Valid date
        return true;
    } else {
        // Invalid date - Not 1st or 16th
        gs.addErrorMessage("Proposed date must be the 1st or 16th day of a future month.");
        return false;
    }
} else {
    // Invalid date - Not a future month
    gs.addErrorMessage("Proposed date must be in a future month.");
    return false;
}
Thanks
Johnny

Please mark this response as correct or helpful if it assisted you with your question.