How do I check if catalog variable date is between today and 7 days from today

Mike LHCG1
Tera Contributor

I have a requirement to check if a catalog variable date is between today and 7 days from today. I haven't found a way to do it with catalog policies. Does anyone have a solution for this that they can share with me? I would appreciate it if in addition to what needs to be done, if you could share how it's done. Thank you.

1 ACCEPTED SOLUTION

jonsan09
Giga Sage

You could utilize catalog client scripts that run while the user fills out the catalog item. See this article with script examples: https://www.servicenow.com/community/developer-articles/date-validations-client-scripts/ta-p/2298860

View solution in original post

3 REPLIES 3

jonsan09
Giga Sage

You could utilize catalog client scripts that run while the user fills out the catalog item. See this article with script examples: https://www.servicenow.com/community/developer-articles/date-validations-client-scripts/ta-p/2298860

VaishnaviK3009
Tera Guru

Hi @Mike LHCG1 !!

To enforce a date between today and 7 days from today, use a Catalog Client Script (recommended: onSubmit).

Example (onSubmit Catalog Client Script):

function onSubmit() {
    var dateStr = g_form.getValue('your_date_variable');
    if (!dateStr) return true;

    var selectedDate = new Date(dateStr);
    selectedDate.setHours(0,0,0,0);

    var today = new Date();
    today.setHours(0,0,0,0);

    var maxDate = new Date(today);
    maxDate.setDate(today.getDate() + 7);

    if (selectedDate < today || selectedDate > maxDate) {
        g_form.showFieldMsg(
            'your_date_variable',
            'Date must be between today and 7 days from today.',
            'error'
        );
        return false;
    }
    return true;
}

 

Mark this as Helpful if it clarifies the issue.
Accept the solution if this answers your question.

Regards,
Vaishnavi
Associate Technical Consultant

AndersBGS
Tera Patron

Hi @Mike LHCG1 ,

 

Why not creating a catalog client script to ensure that the date is between now and 7 days from now? 

 

If my answer has helped with your question, please mark my answer as the accepted solution and give a thumbs up.

Best regards
Anders

Rising star 2024
MVP 2025
linkedIn: https://www.linkedin.com/in/andersskovbjerg/