Set Character limit on Multi-line Variables

Kamva
Giga Guru

 

Hello Everyone,

I'm currently working on implementing a character limit for a multiline variable within a variable set that's associated with a specific catalog item. Despite attempting to utilize the max_length attribute, I haven't achieved the desired result. As an alternative, I've explored the option of employing a catalog client script. However, it seems like its not firing becuase I have tried to insert a dummy alert as well never showed. Please see my script below and advise where you can:

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

 

Below is the configurations:

Kamva_0-1708510339569.png

 

 

Thank you

1 ACCEPTED SOLUTION

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Kamva ,

 

are you checking this on portal?

 if yes please make UI type as all & then try it should work.

 

Thanks,

Danish

View solution in original post

3 REPLIES 3

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Kamva ,

 

are you checking this on portal?

 if yes please make UI type as all & then try it should work.

 

Thanks,

Danish

Aman Kumar S
Kilo Patron

Hi @Kamva 

Can you check if your variable selected in the client script variable is correct?

also, try putting in alerts here, your script looks fine except for using function "showFieldMsg"

Go through below doc to understand the function better.

https://docs.servicenow.com/bundle/washingtondc-application-development/page/script/useful-scripts/r...

 

Best Regards
Aman Kumar

Amit Verma
Kilo Patron
Kilo Patron

Hi @Kamva 

 

I simulated your requirement on my PDI. Below are my findings :

 

1. Ensure that you have selected UI Type as All in your On-Change Client Script as shown below :

 

AmitVerma_0-1708515964860.png

 

2. Change your Client Script as below :

 

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

   //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.showFieldMsg('reason_for_nomination', 'Maximum character limit exceeded. Please limit your input to ' + charLimit + ' characters.');
        return false;
   }else{
        g_form.hideAllFieldMsgs();
        return true;
   }
}

 

Output :

AmitVerma_1-1708516126456.png

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.