- 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-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-21-2014 08:38 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-16-2015 01:25 PM
I have implemented this for just a date (no time), however the 31st of any month will not work. Any ideas?