Client Script onSubmit Date Validation

sandeep6
Tera Contributor

Hi Guys,

 

I am new to snow scripting i have below client script as part of my scoped app

function onSubmit() {
var startDate = g_form.getValue("start");
var endDate = g_form.getValue("finish");
var format = g_user_date_time_format;

if (startDate === "" || endDate === "")
return true;

// get date strings into a number of milliseconds since 1970-01-01
var startDateMs = getDateFromFormat(startDate, format);
var endDateMs = getDateFromFormat(endDate, format);
if (startDateMs < endDateMs)
return true;
g_form.clearMessages();

// 0 from "getDateFormat" means an invalid date string was passed to it
if (startDateMs === 0 || endDateMs === 0) {
if (startDate === 0)
g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("start")));
if (endDate === 0)
g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("finish")));
return false;
}
}

 

When Submitting the record its failing to check start and end dates on the form, any ideas?

At the moment end date can be in the past to start date. Eg. Its allowing below Start Date 26/10/20 End Date 25/10/20 to be submitted rather than validating the dates and making sure end date is after start date.

 

Appreciate all your help.

 

Thanks

Sandeep

1 ACCEPTED SOLUTION

SanjivMeher
Kilo Patron
Kilo Patron

Can you try this

 

function onSubmit() {

g_form.clearMessages();
var startDate = g_form.getValue("start");
var endDate = g_form.getValue("finish");
var format = g_user_date_time_format;

if (startDate === "" || endDate === "")
return true;

// get date strings into a number of milliseconds since 1970-01-01
var startDateMs = getDateFromFormat(startDate, format);
var endDateMs = getDateFromFormat(endDate, format);
if (startDateMs > endDateMs)

{

g_form.showFieldMsg('start', 'Start date cannot be after end date', 'error');

return false;

}



// 0 from "getDateFormat" means an invalid date string was passed to it
if (startDateMs === 0 || endDateMs === 0) {
if (startDate === 0)
g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("start")));
if (endDate === 0)
g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("finish")));
return false;
}
}

 


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

View solution in original post

3 REPLIES 3

SanjivMeher
Kilo Patron
Kilo Patron

Can you try this

 

function onSubmit() {

g_form.clearMessages();
var startDate = g_form.getValue("start");
var endDate = g_form.getValue("finish");
var format = g_user_date_time_format;

if (startDate === "" || endDate === "")
return true;

// get date strings into a number of milliseconds since 1970-01-01
var startDateMs = getDateFromFormat(startDate, format);
var endDateMs = getDateFromFormat(endDate, format);
if (startDateMs > endDateMs)

{

g_form.showFieldMsg('start', 'Start date cannot be after end date', 'error');

return false;

}



// 0 from "getDateFormat" means an invalid date string was passed to it
if (startDateMs === 0 || endDateMs === 0) {
if (startDate === 0)
g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("start")));
if (endDate === 0)
g_form.addErrorMessage(new GwtMessage().getMessage("{0} is invalid", g_form.getLabelOf("finish")));
return false;
}
}

 


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

Willem
Giga Sage
Giga Sage

Where are you running this script? Do you get any errors in the Browser console logs?

If it is on the Catalog, please set the UI Type to the correct value on the Catalog client script. For example UI Type: All

Jack
Tera Guru

Hi Sandeep,

On Orlando, SN has already created 2 UI scripts used for comparing 2 Date Time field

https://<instance>.service-now.com/nav_to.do?uri=sys_ui_script.do?sys_id=a839b6ed0b900300385e440ff6673ac7

https://<instance>.service-now.com/nav_to.do?uri=sys_ui_script.do?sys_id=42518fd3672222004792adab9485ef76

And there is the way that SN is using this script:

onChange
https://<instance>.service-now.com/nav_to.do?uri=sys_script_client.do?sys_id=0a6b13db672222004792adab9485ef0e

onSubmit

https://<instance>.service-now.com/nav_to.do?uri=sys_script_client.do?sys_id=a99629f8ef011100521155aef5c0fb72

 

You can take a look.

 

Hope this will help.

//Together we can change the world