Script to limit max size of string field.

Ian46
Tera Contributor

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

14 REPLIES 14

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. 

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

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

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;
    }
}

find_real_file.png

Yes they would have different max lengths, so thanks for the example!

Hitoshi Ozawa
Giga Sage
Giga Sage

The simplest way is to define a Variable Validation Regex and use it w

find_real_file.png

Variable specification

find_real_file.png

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