Date Validation in ServiceNow

ryan86
Kilo Expert

(I'm new to ServiceNow!)

 

I need to find a way of validating a date entered by a user.

I have created a VS which works on fields which are of the "glide_date_time" type, producing an "Invalid Text" message below the field when "submit" is clicked.

Validate Date and Time - ServiceNow Wiki

 

Our date format is "dd-MM-yyyy HH:mm:ss".

 

However, when creating a form for an end user to complete, there is no option to apply the type "glide_date_time" to a field, only "date" or "datetime".

If I create a field with either "date" or "datetime" as the type, the VS simply does not run and incorrect data can be submitted?

 

All help greatly appreciated

1 ACCEPTED SOLUTION

Hi Ryan,



I think I see what is going on here.   I don't believe Validation Scripts apply to Catalog Variables.   This may have to do with the fact that variable values are stored in a string field in the database (sc_item_option_mtom.value).   You can achieve a similar result by creating a Client Script on field change for each field and a Client Script on submit.



An example On Change Client Script (you may need to change the field name, error message, or date format to your needs):



function onChange(control, oldValue, newValue, isLoading) {
      if (isLoading || newValue == oldValue) {


                  return;


      }



      if (getDateFromFormat(newValue, 'dd-MM-yyyy HH:mm:ss') == 0) {


                  g_form.hideErrorBox('pstart_date');


                  g_form.showErrorBox('pstart_date', 'Date is invalid');


      }


      else {


                  g_form.hideErrorBox('pstart_date');


      }
}




An example On Submit Client Script:



function onSubmit() {


        if (getDateFromFormat(g_form.getValue('pstart_date'), 'dd-MM-yyyy HH:mm:ss') == 0) {


                  return false; // Prevents the form from submitting


        }



        if (getDateFromFormat(g_form.getValue('pend_date'), 'dd-MM-yyyy HH:mm:ss') == 0) {


                  return false; // Prevents the form from submitting


        }



        if (getDateFromFormat(g_form.getValue('other_field'), 'dd-MM-yyyy HH:mm:ss') == 0) {


                  return false; // Prevents the form from submitting


        }


}


View solution in original post

12 REPLIES 12

tltoulson
Kilo Sage

You should be able to create Validation Scripts for Date and Date/Time field types as well which will work on your date and date time fields.   There are numerous field types for dates and times in ServiceNow (duration, due date, date, and datetime just to name a few).   You have to match the Validation Script's type to the correct field type or it will not run.


Thanks for the info Travis.



I have since created a VS for 'date' and 'date_time' but the issue still remains.



We want users to be able to type in a date, say 2 years ahead, rather than needing to click the date dialog box one month at a time. However, if we allow them to enter a date, it accepts dates such as "31/02/2016" (which will never exist).



Do VS only run server side? Ideally, I would like to alert the user to the incorrect date, before the data/form is submitted.


Hi Ryan,



Validation Scripts run Client Side when a user clicks Submit or Save before the form is sent to the server.   You can test this by putting an 'alert("Validation Script Running");' statement in the VS.   When you click submit, you'll get the alert right away.   Could you please share the Validation Script you are using?


ryan86
Kilo Expert

The script we are using is the same as the one at the link below (except the date format).



http://wiki.servicenow.com/index.php?title=Validate_Date_and_Time



The validation does not work when the form is posted from the web portal?


I have applied the VS to the data types (date, datetime and glide_date_time).



I am new to ServiceNow, and my predecessor left very little in terms of documentation.



Thanks again,


Ryan