Word limit for Multi line text variable in Record Producer

Sharon24
Tera Contributor

Hi,

I'm required to set the word limit to 1500 max on a Multi Line Text variable in Record Producer. Here is what I did, but it doesn't work.

find_real_file.png

7 REPLIES 7

Filipe Cruz
Kilo Sage
Kilo Sage

Hi Sharon,

If you're setting a limit of 1500 words and you are doing the validation on an onChange client script, I think you will have a lot of script executions on the client side.

I would try to change the requirement to focus on the number of characters instead of number of words. That is an OOB feature and you don't need to do nothing to use it.

If the requirement still persists, you can do the validation onSubmit. The downside is that your users might write a huge text and only on submit they will know that they wrote more than expected!
This can be bypassed if you set an infoMessage on that field information that the max word count is 1500.

The validation can consist simply in something like this:

var words = g_form.getValue("your_field").split(" ").length;
if(words > 1500){
  //inform user
}

You can simply do a split by the " " (space) character, to catch the different words.
In the end, use your code but change the "split" input.

Try it out and check if that works! And check if you want to use an onChange or an onSubmit Client script.

Please, don't forget to mark my answer as correct if it solves your issue or mark it as helpful if it is relevant for you!

Best Regards,

Filipe Cruz

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi Sharon,

Following onLoad script will add an error message to multi-line text field if number of characters is more than max length in UI. Additional script is required to make it work on Service Portal.

The sample sets max length to 10 characters.

function onLoad() {
    var fieldName = 'multiline_text';  // name of multiline text field
    var maxLength = 10;  // max length of multiline text field
	try {
        var control = g_form.getControl(fieldName);
        control.onkeyup = function isMaxLength() {
            var control = g_form.getControl('multiline_text');
            if (control.value.length > maxLength) {
                g_form.hideErrorBox(fieldName);
                try {
                    g_form.showFieldMsg(fieldName, 'Please limit number of characters to ' + maxLength + ' characters.', 'error');
                } catch (e) {}
                control.value = control.value.substring(0, mLength);
            } else {
                g_form.hideErrorBox(fieldName);
            }
        };
    } catch (e) {
        alert(e.message);
    }
}

Execution result:

find_real_file.png

Thanks Hitoshi

But it doesn't work for me. I even created a multiline text variable which was exactly same as 'multiline_text', and copy paste your script. Still doesn't work. Is there anything else I need to configure?

As I've mentioned in my earlier reply, the script won't work in Service Portal.

Is there any error?

Variable page. Make sure the "fieldName" in the script is set to the value of "Name" and not the "Question".

find_real_file.png

Client Script page:

find_real_file.png