- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 08:21 AM
Hi Team,
I am trying to restrict valid to should not be more than one year, i am writing the below client script. But when i am saving the form it is allowing to submit the form with more than one year date.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var validityField = 'valid_to';
var onYearValidity = new Date(new Date().setFullYear(new Date().getFullYear() + 1));
var newValidity = new Date(newValue);
if(newValidity > onYearValidity) {
//g_form.clearValue(validityField);
g_form.showErrorBox(validityField,'Valid to date must be within a year from today');
} else {
g_form.hideErrorBox(validityField);
}
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 09:03 AM - edited 02-20-2024 09:07 AM
Please create an onSubmit client script with the below script, the script will not let the form submit if the valid to date is more than one year.
function onSubmit() {
var validityField = 'valid_to'; // Change this to the actual field name
var newValue = g_form.getValue(validityField);
var onYearValidity = new Date();
onYearValidity.setFullYear(onYearValidity.getFullYear() + 1);
var newValidity = new Date(newValue);
if (newValidity > onYearValidity) {
g_form.showFieldMsg(validityField, 'Valid to date must be within a year from today', 'error');
return false; // Prevent form submission
} else {
g_form.hideFieldMsg(validityField);
return true; // Allow form submission
}
}
If my response helped you, please mark it as correct or helpful.
If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 08:31 AM - edited 02-20-2024 08:35 AM
Please review - https://www.servicenow.com/community/csm-forum/valid-to-field-should-display-message-when-is-set-to-...
You can easily create a UI Policy to check if the valid to date is within a year or not.
If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-20-2024 09:03 AM - edited 02-20-2024 09:07 AM
Please create an onSubmit client script with the below script, the script will not let the form submit if the valid to date is more than one year.
function onSubmit() {
var validityField = 'valid_to'; // Change this to the actual field name
var newValue = g_form.getValue(validityField);
var onYearValidity = new Date();
onYearValidity.setFullYear(onYearValidity.getFullYear() + 1);
var newValidity = new Date(newValue);
if (newValidity > onYearValidity) {
g_form.showFieldMsg(validityField, 'Valid to date must be within a year from today', 'error');
return false; // Prevent form submission
} else {
g_form.hideFieldMsg(validityField);
return true; // Allow form submission
}
}
If my response helped you, please mark it as correct or helpful.
If my response helped you, please click on "Accept as solution" and mark it as helpful.
- Saloni