- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-14-2014 06:42 AM
(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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2014 06:34 AM
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
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2014 07:12 AM
I've tested this on my instance and have been unable to replicate. When I use those scripts on Date and Date/Time, fields are validated correctly. Here is my setup:
Date Validation Script:
Date/Time Validation Script:
Date Field is Validated:
Date/Time Field is Validated:
So, based on my setup, do you see something different in yours? Are you validating on a standard ServiceNow form, a Catalog Item form, or some other custom form? Are you running any Client Scripts that may be interfering with the Validation Scripts?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2014 12:38 AM
Hi Travis,
Apologies, I should have mentioned I am validating a form completed in the web portal, which is displayed to users. It is a custom form. The fields on the form have data types set as "datetime".
With the VS in place, the validation runs on forms from within ServiceNow (like what your screenshots show) on my system too.
How can I apply the same VS script to the custom form?
Currently, If a user enters the date as 30/02/2014, the form submits and the field is BLANK (" ") in the database. If I change the field to "date", it only allows the user to select a date from the on screen datepicker, which ensures a valid date is selected. However, if they wanted to submit a date that may be in a couple of years, they need to click the > arrow (12 times for one year, 24 for two years etc. .... ), which becomes a bit of a nuisance and is not very user friendly.
Thanks again for your input on this,
Ryan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-19-2014 08:03 AM
Hi Ryan,
I'm not entirely sure on this one. The Validation Scripts are applying on the regular Glide Forms so that is good. Could you perhaps provide a screenshot and code sample from the form you are using? I have never used VS's outside of regular forms so I am not sure if this would be something available via configuration or if this would require further customization. But if I could see some sample of your work, perhaps I could help find a solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2014 12:59 AM
HI Travis,
I have a "Record Producer", which has 21 variables listed within it. Below is a screenshot showing the 3 date fields I need to validate.
Each needs the user to be able to EITHER select the time from the datepicker, OR input the time manually. However, when entered manually, I need a script to run to validate the date entered is valid (IE. reject dates like 30/02/2014 or 55/55/2014).
At present, it accepts an invalid entry, but the date found in the tables is as follows:
If I enter 30/02/2014, the date actually stored is 02/03/2014, so the 2 "extra" days are applied and the month incremented.
The only other solution I can see, is being able to add a button to the datepicker which allows the user to increase by one year, rather than only by one month?
Thanks again!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-21-2014 01:00 AM