Date field Validation to restrict selecting past dates

Rocky5
Kilo Sage

Hi Experts,

I have written a onchange catalog client script on the "Start Date" variable to restrict users from not to select the past dates. The below is the code I am using, But the code is also triggering if the selected date is today's date (current date). All I need to is restrict user to not to select any date before current date. 

code: 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

//Type //get the entered date string
var enteredDate= g_form.getValue('start_date');
//get the current date/time
var rightNow = new Date().valueOf();
var dateToCheck = new Date(enteredDate).valueOf();
if (dateToCheck < rightNow)
{
//Display Error Message
g_form.addErrorMessage(getMessage('Select appropriate date'));
g_form.setValue('start_date','');
}
}

Any ideas on how to achieve this ? 

 

Thanks in advance,

Rocky.

10 REPLIES 10

Just simulated with Variable Set. Works fine.

So is the alert not triggered? The setValue not? Both not? And when strange instance things are happening... cache.do 🙂

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

The alert and set value both are triggered irrespective of the date I select. Even if select today's date or future date the alert and set value is being triggered. 

 

Thanks,

Rocky.

Hmm no clue. Same set up works fine here 😞

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Mark Roethof
Tera Patron
Tera Patron

For example:

image

image

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark
2020 ServiceNow Community MVP
2020 ServiceNow Developer MVP

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

But in Agent wworkspace is not reflecting