Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to restrict variable

pramn
Kilo Sage

Hello team , how do i restrict multiline variable with 10words in catalog item ?

3 REPLIES 3

Allen Andreas
Tera Patron

Hello,

You can use Regex to accomplish this on the MVRS, such as here: https://www.servicenow.com/community/developer-forum/how-do-i-restrict-length-of-characters-of-varia...  -- but this is for character count, but the same can be done for words, such as: https://stackoverflow.com/questions/557695/limit-the-number-of-words-in-a-response-with-a-regular-ex... 


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Brian Lancaster
Kilo Patron

On Change client script. You will need to something like this.

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }

    //Type appropriate comment here, and begin script below
	var text = newValue.toString().Split(" ");//sprit sting into an array by space between words
	if (text.length > 10){//Check length of array is greater then 10
		alert("No more then 10 words can be in filed");
		g_form.clearValue("variable name");
	}
}

Sumanth16
Kilo Patron

Hi @pramn ,

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }

	alert('Testing...');
   //Type appropriate comment here, and begin script below
   var charLimit = 250;
   var reasonForNomination = g_form.getValue('reason_for_nomination');

   if (reasonForNomination.length > charLimit){
		g_form.setValue('reason_for_nomination', reasonForNomination.substring(0, charLimit));
		g_form.addFieldMessage('reason_for_nomination', 'Maximum character limit exceeded. Please limit your input to ' + charLimit + ' characters.');
        return false;
   }else{
        g_form.clearMessages();
        return true;
   }
}

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

 

Thanks & Regards,

Sumanth Meda