- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2019 06:20 AM
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
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2019 06:25 AM
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

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2019 06:23 AM
Hi there,
What is the reason you are trying this scripted? Have you already looked at:
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-25-2022 01:34 AM
Thanks Mark for sharing this quick solution . Much appreciate!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2019 06:25 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-25-2019 06:29 AM
Thank you Mark - completely forgot about default value/attributes.