Skip to main content
Participating Frequently
Apr 27, 2023 6:27 PM UTC
Question

How can I limit the amount of words in a text field?

I need to limit words in a text field instead of characters. I found this script in another forum, but the count seemed to be inaccurate:

var maxWords = 10;

if (AFMergeChange(event).split(" ").length > maxWords) {

  app.alert("The max word count is " + maxWords + " words.");

  event.rc = false;

}

 

Any help is appreciated.

This topic is closed to new replies. Start a new post to keep the conversation going.

3 replies

try67
Community Expert
Community Expert
Apr 27, 2023 9:45 PM UTC

This code is far from perfect, but it would require a more complicated code to achieve a better result.

For example, is "far-fetching" two words or just one? The code above will consider it to be one. It will also not take line-breaks into account, in case you have a multi-line field.

Translate
Nesa Nurani
Community Expert
Community Expert
Apr 27, 2023 7:34 PM UTC

Use it in 'Format' tab as 'Custom keystroke script'.

The script will disappear when you enter it, but it will still work, or if you don't want it to disappear you can add the script at document level and call the function from 'Custom keystroke script'.

The script will count the number of spaces, so if you connect two words with a dot or comma without space, it will count it as one word.

Translate
Participating Frequently
Apr 27, 2023 7:40 PM UTC

Thanks. I will try this tomorrow.

Translate