Restrict user to select past date in catalog item

Sachin G K1
Kilo Sage

Hi All,

 

How to restrict the user from selecting past dates. I used below methods for restricting user from selecting past dates. It works fine if use past date of current month. And doesn't work for previous and future months.

 

i,e if current date is 19-3-2023, below methods works fine according for any date in current month. But if i select 20-2-2023,its not showing error instead its accepting it. And if i select 18th in next month its showing error "not to select past date"

 

1. Used ui policy

Execute if true:

function onCondition() {
g_form.clearValue("date_variable");
g_form.showFieldMsg("date_variable", "Previous date not allowed" , "error");
}

 

2.Used catalog client script

 

var curDate = ("0" + new Date().getDate()).slice(-2) + '-' + ("0" + (new Date().getMonth() + 1)).slice(-2) + '-' + new Date().getFullYear();


if (curDate > newValue) { 
alert("Selected date shouldnt be less than current date");
g_form.clearValue('start_date');
}

 

 

 

 

Thanks in Advance

1 ACCEPTED SOLUTION

Hi Gujan,

 

Its working fine now, how to ensure that end date should always be greater than start date using ui policy

View solution in original post

9 REPLIES 9

Hi @Sachin G K1 

Please check below article. You can use the Last mentioned scenario in the below article:-

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...  


Please Mark My Response as Correct/Helpful based on Impact
Regards,
Gunjan Kiratkar
2X ServiceNow MVP
Community Rising Star 2022
Youtube : ServiceNow Guy

Hi Gunjan,

 

I tried it before, i am using sandiego version. it didn't work.

@Sachin G K1 

you can apply UI policy condition as if start date - is more than - 0 days - after - end date then clearValue() and an error message.

 

Regards,

Pradnya

Jaspal Singh
Mega Patron
Mega Patron

Hi Sachin,

Unsure, your need for script but this can be handled with no code at all. Mark has a solution for it. Please check, link

 

Giles Lewis2
Kilo Expert

You cannot write a client script that makes hard-coded assumptions about date format and expect it to work reliably because users have the ability to change their date format.