[Service Portal] How to validate start and end dates in portal

Paul Bayani
Mega Expert

Hi! Appreciate your advice about how to correctly put a date validation in the servical portal.

What I'm trying to achieve:

There are 2 dates in the portal form:

Start Date

End Date

Validation: The end date should not be prior the start date

I tried that using below client script but it did not work.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var first=g_form.getValue('u_effective_date_of_leave');
var last=g_form.getValue('u_last_worked_day');

if(last<=first){
alert("Last day worked should be on or prior to effective date of leave");
g_form.setValue('u_effective_date_of_leave','');
//Type appropriate comment here, and begin script below
}
}

Anything I'm missing? thanks in advance

11 REPLIES 11

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 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

And a copy/paste from the article:

Comparing two entered dates with each other. Date 2 should not be before Date 1 (image, planned end date should not be before planned start date)

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

Hello Mark! Thanks for sharing this.

I used the Catalog UI Policy route for the 2 date fields I'm creating (comparing 2 entered dates). So what would I add then in the Catalog UI Policy actions for this one? Like where do I enter the error message.

Thanks,

Paul

Have a look at the article, you can generate the message through the Script fields if true, if false.

No catalog UI policy actions needed.

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