The CreatorCon Call for Content is officially open! Get started here.

Setting Max Length on a Catalog Item Variable

NonaJohnson
Kilo Expert

Hi,

I am trying to create a maxLength script on a catalog item variable that will work like the attribute used on dictionary field where they set the attribute on the field to max_length=50.  Here the text just stops after that number of characters.  I'd like to crate that same functionality on a catalog item variable in an OnChange client script (or do I need to use onLoad). The variable only shows after another selection is made so thought the onChange script would be needed.

I've tried:

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

var maxLength = 30;
var descr = g_form.getControl('application_description');
if(descr.value.length > maxLength){
g_form.setValue('application_description','');
alert('Please enter only 30 characters or less');

}

}

 

And just a standard onChange to just give me the alert

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

//Type appropriate comment here, and begin script below
if(newValue.length > 30){
g_form.setValue('application_description','');
alert('Please enter only 30 characters or less');
}
}

 

What they really want is for the user to not be able to input more than 30 char - i.e. text no longer shows like we can do on a dictionary field.

How can I achieve this

Thanks

Nona Johnson

1 ACCEPTED SOLUTION

Mark Roethof
Tera Patron
Tera Patron

And if it's about just a validation, have you considered Variable Validation Regex instead? Almost no coding needed to achieve the same.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

View solution in original post

4 REPLIES 4

Mark Roethof
Tera Patron
Tera Patron

Hi there,

What is the reason you are trying this scripted? Have you already looked at:

find_real_file.png

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

Thanks Mark for sharing this quick solution . Much appreciate!

Mark Roethof
Tera Patron
Tera Patron

And if it's about just a validation, have you considered Variable Validation Regex instead? Almost no coding needed to achieve the same.

If my answer helped you in any way, please then mark it as helpful.

Kind regards,
Mark

---

LinkedIn
Community article list

 

Kind regards,

 

Mark Roethof

Independent ServiceNow Consultant

10x ServiceNow MVP

---

 

~444 Articles, Blogs, Videos, Podcasts, Share projects - Experiences from the field

LinkedIn

NonaJohnson
Kilo Expert

Thank you Mark - completely forgot about default value/attributes.