Multiline Variable in catalog item

Snehal
Tera Expert

Hi Team 

 

How to restrict the users to add more than 5 points in multi line field in catalog item .ex question called "Description of the application" it include only 5 points line

1.

2.

3.

4.

5. 

help me to achieve this requirement .attached an attachment where i can't able to add more than 5 .

 

Thanks in advance !

 

2 REPLIES 2

Vishal Jaswal
Giga Sage

Hello @Snehal  

You need onChange Catalog Client Script:

function onChange(control, oldValue, newValue, isLoading) {
 if (isLoading || newValue == '') {
   return;
 }
 // Split the input by newlines and count lines
 var linesCount = newValue.split('\n');
 if (linesCount.length > 5) {
   // Trim to first 5 lines
   var trimmedLinesCount = linesCount.slice(0, 5).join('\n');
   g_form.setValue('description_of_the_application', trimmedLinesCount);
   alert('You can only enter up to 5 points.');
 }
}

VishalJaswal_2-1745515741083.png


NOTE: The catch is: user may end up typing let's say 10 lines or points and he/she will not be prompted with anything however the moment click to select any other field to populate value then only the alert will pop-up.

Validation Result:

 

VishalJaswal_1-1745515699363.png

 


Hope that helps!

Ankur Bawiskar
Tera Patron
Tera Patron

@Snehal  

you can use onChange catalog client script for this

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue === '') {
        return;
    }
        g_form.hideFieldMsg('description_of_the_application'); // your variable name
    var lines = newValue.split('\n');
    
    if (lines.length > 5) {
        g_form.showFieldMsg('description_of_the_application', 'You can only add up to 5 points.', 'error');  // your variable name
        g_form.clearValue('description_of_the_application');  // your variable name
    } 
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader