Is it possible to add minimum character required for 'Multi-line text' variable type in Service Catalog?

2022_ServiceNow
Tera Expert

Hi everyone,

Is it possible to add the minimum characters required to the catalog field of variable type multi-line text?

Eg: I want a user to at-least write 30 characters before submitting.

Regards,

Priya

1 ACCEPTED SOLUTION

Muhammad Khan
Mega Sage

Use onSubmit()/onChange() catalog client script as per your requirement.

function onSubmit() {

    if(g_form.getValue('<multi_line_variable_backend_name>').length < 30){
		g_form.showFieldMsg('<multi_line_variable_backend_name>', 'Enter at least 30 characters.', 'error', true);
		return false;
	}
}

View solution in original post

15 REPLIES 15

Pavankumar_1
Mega Patron

Hi,

Yes you can validate the length of the variable while submitting the form.

1. Create a onSubmit catalog client script on your catalog item.

try below script

you can add alert or infomessage based on your requirement you can add.

 

function onSubmit() {

    var minLength = 30; //Define the length here
    var field1 = g_form.getControl('description'); //Replace description with your variable name
    if (field1.value.length < minLength) {
       // g_form.addInfoMessage('description should not be less than 30 characters');
        alert('Description should not be less than 30 characters');
        return false;
    }
}

 

Hope you it helps you.

Please Mark ✅ Correct/helpful if applicable, Thanks!! 

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Hi,

Thank you for the response.

This is working fine when I try it in the ServiceNow catalog.. but in the Service Portal, it's not working, I can submit the form with less than 30 characters also.

Regards,

Priya

Hi,

No it will work in portal as well but you need to select UI type as All in Catalog client script.

 

Thanks,

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Pavankumar_1
Mega Patron

Hi @Priya

If it is resolved please close the thread by Marking as ✅ Correct so that other readers will get benefit if they have similar question. In future they will refer this question will see solution.

 

Hope you understood.

Happy learning!

 

Regards

Pavankumar

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

Murthy Ch
Giga Sage

Hi Priya,

Instead of writing a client script you can actually achieve just by using Variable validation regex.

Create a record in question_regex table and call it in variable. See below for your reference:

^(?=.*\d).{30,}$

find_real_file.png

Variable screenshot:

find_real_file.png

Output:

find_real_file.png

Hope it helps.

 

Thanks,

Murthy

Thanks,
Murthy