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

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


        }


}


Hi Travis,



Thanks for your help with this. I have adapted your onSubmit() script to meet our needs, with a small piece of additional functionality (flash) and it works as we required it too.



Thanks again,


Ryan


I have implemented this for just a date (no time), however the 31st of any month will not work. Any ideas?