Restrict knowledge article valid to field

Atchutaram
Tera Contributor

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);
   }

}

1 ACCEPTED SOLUTION

Saloni Suthar
Mega Sage
Mega Sage

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

View solution in original post

2 REPLIES 2

Saloni Suthar
Mega Sage
Mega Sage

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

Saloni Suthar
Mega Sage
Mega Sage

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