Validate Variable Date Fields

Chris_Wilkinson
Tera Expert

Hi, 

I am trying to setup two date fields which have onChange client scripts to add validation

 

'Needed From' has a client script which makes sure the date cannot be set in the past and is working.

'Needed until' uses the script below and should check to see if the date entered is after the date in the 'needed from' field. This seems to work if the dates are within a week of now but then stops working if the dates are beyond a week

Any advice would be great

_____________

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

   //Type appropriate comment here, and begin script below
    var startDate = new Date(g_form.getValue('var_needed_from'));   //get the value of the start_date field using the g_form.getValue method
    var checkEnd = new Date(newValue);                           // create a new JS date from the desired end_date field (using newValue parameter)
       
    if (checkEnd <= startDate){                                //Again - a simple JS Date object comparison
       alert('End date must be after start date');
       g_form.setValue('var_needed_until','');
    }
}

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Have you considered using a (Catalog) UI Policy to achieve date validations? There's almost no-code needed to achieve date validations this way. Have a look at an article I wrote on this:
No Code date validations thru (Catalog) UI Policies

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

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

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

View solution in original post

2 REPLIES 2

Mark Roethof
Tera Patron
Tera Patron

Hi there,

Have you considered using a (Catalog) UI Policy to achieve date validations? There's almost no-code needed to achieve date validations this way. Have a look at an article I wrote on this:
No Code date validations thru (Catalog) UI Policies

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

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

---

LinkedIn
Community article, blog, video list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

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

LinkedIn

Hi Mark, 

Thanks for the suggestion, this worked perfectly 🙂

Warm Regards

Chris