Set Character limit on Multi-line Variables for 4 variables

LearnerSN
Tera Contributor

Set Character limit on Multi-line Variables for 4 variables : We have 4 different variables in a catalog form where we have to set limit for them for eg : Description(400), Name(250), Stock(350), Lines(380) how to achieve this pls let me know with less code and as per the best practise.

 

 

14 REPLIES 14

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || !newValue) return;
    if (newValue == oldValue) return;

    var inputText = newValue.trim();
    g_form.hideFieldMsg('description');

    // 1. Max length check (Matches your mailbox structure)
    if (inputText.length > 400) {
        // Automatically trim the input back down to 400 characters immediately
        var validText = inputText.substring(0, 400);
        g_form.setValue('description', validText);

        // Fetch the translated message to pass Instance Scan health checks perfectly
        g_form.showFieldMsg('description', getMessage('ellipse.cat.err.desc'), 'error');
    }
}
 
How to change it for 4 variables

Hi @LearnerSN,

 

If you are doing onChange client script, then you need one Client script for each. 

 

If this response resolved your issue, please consider marking it as Helpful and accepting it as the Solution. Your feedback helps other ServiceNow Community members find reliable answers.

Kind Regards,

Ehab Pilloor

ok can we do it one catalog cleint script, what is the way

@LearnerSN,

 

No, you cant do with only one client script. Each field needs its separate validation, its dependent on itself, there is no issue in doing 4 client scripts.

There is an alternative to it if you dont want that much client scripts.
Go to All > Variable Validation Regex

EhabPilloor_0-1793358567237.png

Create a new record
400 char limit

Add this in regex: ^[\s\S]{0,400}$

Add Validation message: Input is exceeding 400 characters
Save the record.

In the variable record, go to Type Specifications section > Validation Regex field, add this Validation Regex you created.

 

No script needed, it will validate 400 character limit.

 

If this response resolved your issue, please consider marking it as Helpful and accepting it as the Solution. Your feedback helps other ServiceNow Community members find reliable answers.

Kind Regards,

Ehab Pilloor

 

Hi @LearnerSN 

 

Could you please confirm, post setting 4 variables in the multiline variable , is that multi-line variable would be ReadOnly?

If that is the case , you can validate length for each 4 variable using onChange client scripts and then set the value to Multiline variable. 

  Is your requirement includes Multiline variable should be editable ? In that case you