In incident form only today date field iwant not previous date and future date by using on submit

patanfarjana110
Tera Contributor
 
5 REPLIES 5

Amit Verma
Kilo Patron
Kilo Patron

Hi @patanfarjana110 

 

So you want to restrict the user to select only Today's date in a date time field on the incident form ? If yes, on which field you want to enable this restriction ?

 

You can refer below posts which could be helpful :

https://www.servicenow.com/community/developer-forum/date-field-to-only-allow-current-date-and-futur...

https://www.servicenow.com/community/itsm-forum/how-to-prevent-user-to-select-past-date-and-time-of-...

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

 

Thanks & Regards

Amit Verma


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

can we connect

Danish Bhairag2
Tera Sage
Tera Sage

Hi @patanfarjana110 ,

 

Why do u want to go with onSubmit script when u can achieve this with less code via a UI policy.

In UI policy add condition if selected field is after today's date or before today's date n under script tab just mention run script as All n under execute if true u can just print an alert stating " Only Today's date can be selected"

 

But if u still want to go with onSubmit script

Plz try below code n check if it works

 

function onSubmit() {
var todayDateField = g_form.getValue('today_date_field'); // Replace 'today_date_field' with the actual field name
var currentDate = new Date().setHours(0, 0, 0, 0); // Get the current date with time set to 00:00:00:000
var selectedDate = new Date(todayDateField).setHours(0, 0, 0, 0); // Get the selected date with time set to 00:00:00:000

if (selectedDate !== currentDate) {
alert('Please select today\'s date.'); // Display an alert if the selected date is not today
return false; // Prevent form submission
}

return true; // Allow form submission
}

 

Thanks,

Danish

Sohail Khilji
Kilo Patron
Kilo Patron

Hi @patanfarjana110 ,

 

Go to the field dictionary > default value

Set the value to :

javascript:gs.glideDatenow()

 

Why go with Onsubmit client script?


☑️ Please mark responses as HELPFUL or ACCEPT SOLUTION to assist future users in finding the right solution....

LinkedIn - Lets Connect