Script to limit max size of string field.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-03-2020 07:54 AM
Hi,
I have many string fields set to 400 characters. This allows the boxes to be the right size. However I need to stop users putting in too much data. How best to do this with a script?
I tried an On Change Client script, but it didn't work:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '')
{
return;
}
if(newValue.length>4000)
{
g_form.showErrorBox("your_field","You cannot enter more than 4000 characters",true);
}
}
Can anyone suggest the best way to achieve this?
Thanks Ian

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-03-2020 11:50 AM
Sample code:
var arr = ['description','short_description']; // add your field name in array
for(var i = 0; i <arr.length ; i ++){
if(g_form.getValue(arr[i]).length > 4000){
alert('hey its more than 4000');
return false;
}
}
Note: Add field name in an array.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-03-2020 08:45 PM
Hi Ian,
As suggested you would require onSubmit to stop user from submitting the form.
Also for multiple fields you can use this
function onSubmit(){
var finalArr = [];
var arr = ['description','short_description']; // add your field names in array
for(var i=0;i<arr.length;i++){
if(g_form.getValue(arr[i]).toString().length > 4000){
finalArr.push(arr[i].toString());
}
}
if(finalArr.length > 0){
alert('You have crossed the character count for these fields ' + finalArr);
return false;
}
}
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-03-2020 09:12 PM
Since fields probably have different max length, fixed the code. In the following code, field name "description" has max length of 400 characters while field short_description has max length of 100.
function onSubmit() {
var messageArray = [];
var fieldArray = [
{'name': 'description', length: 400},
{'name': 'short_description', length: 100}
]; // add field names and length
for (var i = 0; i < fieldArray.length; i++) {
if (g_form.getValue(fieldArray[i].name).length > fieldArray[i].length) {
messageArray.push('max length of field ' + fieldArray[i].name + ' is ' + fieldArray[i].length + '\n');
}
}
if (messageArray.length > 0) {
g_form.addErrorMessage('You have crossed the character count for these fields.\n' + messageArray);
return false;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-04-2020 06:18 AM
Yes they would have different max lengths, so thanks for the example!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎12-03-2020 03:41 PM
The simplest way is to define a Variable Validation Regex and use it w
Variable specification
If using client script, there's a need to create a onChange script and onSubmit script to check the length.
onClient script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
if(newValue.length > 4000) {
g_form.showErrorBox("field_1","You cannot enter more than 4000 characters",true);
}
}
function onSubmit() {
if(g_form.getValue('field_1').length > 4000) {
g_form.addErrorMessage("You cannot enter more than 4000 characters",true);
return false;
}
}
onSubmit script