Skip to main content
Inspiring
March 30, 2021
Answered

How to count or limit the number of words in a text box?

  • March 30, 2021
  • 7 replies
  • 7107 views

Hello everyone,

 

I want to create a textbox that will be filled everytime the user writes an answer.

The first option of make this text box to accept only the total number of words that fit the answer. After that, the keyboard will not responde anymore.

Another alternative (I think this is more advanced) would be to track each of the letters

the user inputs and in case it is a wrong one, the keyboard will not respond, until the right

choice of letters is made.

Can you please guide me towards achieving this goal? I would appreciate receiving your kind instructions/tips on that.

This topic has been closed for replies.
Correct answer andres45888027

Hi,

 

If you have a limited number of answers would a drop down list not be a better opption? just a thought.


I decided to use three radio buttons, since the idea is to lay out all the options at once.

7 replies

Thom Parker
Community Expert
Community Expert
March 31, 2021

It seems that you are asking to match the specific text entered by the user? As they enter it?

In this case you'll need to use a custom keystroke script, which can track the exact keystroke and text entries.

 

You can read more about it here:

https://www.pdfscripting.com/public/Formatting-Form-Fields.cfm#KeyStr

 

The JavaScript model provides event properties for finding the total entry and rejecting or accepting keystrokes.

 For example, here is the entry for event.selEnd, which provides some sample code.

https://help.adobe.com/en_US/acrobat/acrobat_dc_sdk/2015/HTMLHelp/#t=Acro12_MasterBook%2FJS_API_AcroJS%2Fevent_properties.htm%23TOC_selEndbc-13&rhtocid=_6_1_8_27_4_12

 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
April 1, 2021

Thanks for you answer.

 

I did some research and wrote the following script, but its not working.

I keep getting a message saying that "avocado is not defined".

 

event.value = [avocado];
var re = event.value;

if (event.willCommit == false) {

if (re.test(event.change) == false) {
app.beep()
event.rc = false;

}

}

Thom Parker
Community Expert
Community Expert
April 1, 2021

So first, "avocado" is a literal text string, so it needs to be quoted. Other than this,  the script doesn't do what you think it should do.   But I think you have the right idea, which is to use some kind of string compare. However, rather then hardcoding the test, you need a general technique that will take any phrase and match it to the user input.  One way to do this is to use the string.indexOf() function. this returns the first index of  one word with in another.  

For Example:

 

var strTest = "The sky it blue";
if(!event.willCommit)
{
    event.rc = !event.change || (strTest.indexOf(event.value) == 0) 
                && (event.change == strTest[event.value.length]);
}

 

 

In this codee, the expected phrase is strTest.    The event.value property holds the string already entered. It must be at the 0 index of the test string, or the user has not entered it correct. And event.change is the keystroke they just hit. It must be the next letter in the phrase. 

The first condition "!event.change", covers the case where the user backspaces or deletes. 

 

However, this code does not cover all conditions. It's close, but imagine the scenario where the user selects several letters in the middle of the entered phrase and hits the delete key, or pastes text. In this case the script will fail and put the keystroke event into a state that cannot be easily recovered from. 

 

Really what you need to do is to construct the finished text after the current keystroke, as shown in the event.selEnd example.  

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
try67
Community Expert
Community Expert
March 31, 2021

I've created a (paid-for) tool that allows you to add a "live" words counter next to your field:

https://www.try67.com/tool/acrobat-live-characters-and-words-counter

It can also be adjusted to limit the user input when reaching a certain number of words. You can contact me privately via [try6767 at gmail.com] to discuss that option.

Inspiring
April 1, 2021

Thanks anyway, but this is not exactly what I´m looking for.

Nesa Nurani
Community Expert
Community Expert
March 31, 2021

It would be helpful if you could describe some more what you want.
If I got you right I belive your second option is actually easier one, if you
want specific answer, for example lets say an answer is "Option1" you can just use app.alert and if
user didn't write that answer alert pop: "Your answer is wrong, please try again",
not recommended but if you really want to block them set focus back to field until they got answer right.

Inspiring
April 1, 2021

Hi,

 

I´m using the script posted previously in "Custom Keystroke Script" section. 

Nesa Nurani
Community Expert
Community Expert
April 1, 2021

Thank you very much!!

I am going to test the code and will get back to you, but it looks very smart and straighforward.

One more question: In case there´s a space between the words, I understand I should write this /^[" "]+$/

In other words, a set of empty inverted commas will go between the square brackets, right?

 


Yes, it should be easy enough to modify code to your needs.

Inspiring
March 31, 2021

Just to add up, there is couple of issues i see in this post.
1)I want to create a textbox that will be filled everytime the user writes an answer.
textbox and text fields are not same. Where is the answer filled? and with what should 'textbox' be filled?

2)keyboard will not responde anymore
How will user correct his answer if keyboard not respond anymore?

3)BarlaeDC suggestion will work but what if there is multiple answers with different size?
Or you use just one question/answer per field?

Inspiring
April 1, 2021

Thanks for you answer.

Sorry if my text was not clear.

1. By "textbox" I meant "texfield". I ormally use them as synonyms.

2. By "keyboard does not respond"  I mean the key board will not show the letter, in case it does not match 

the letter included in the target word. For instane. Suppose that my target word is "lettuce". The user will 

will have to type all the letter in the righr sequence:  l, e, t, ,t, u, c,e  etc. In case he types "o", instead of an

"t", the keyboard will not respond until the right letter is chosen again.

3. There will be only one option (word) per each textfield

ls_rbls
Community Expert
Community Expert
March 31, 2021

+++Adding to the discussion,

 

If you need to fill a text field based on an answer that is provided to the user by selecting several words (not individual alphanumeric characters), shouldn't it be easier to provide the users with a dropdown menu or a listbox that contain a list of words to choose from?

 

And in order to be able To count words in a tex field object you'll also need to use custom calculating JavaScript script.

 

The built-in feature to lmit the number of characters in a textfield doesn't perform a count of words.

 

Please clarify what exactly is the goal that you're trying to achieve.

 

The scripting involved with what you're inquiring about may not be a trivial task.

Inspiring
April 1, 2021

Thanks for the reply. I know it is not a trivial task, but I guess it can be done.

My goal is to create a script that checks every keystroke of the user to see if it matches

with a given target word assigned for the textfield, before the text is commited (enter key or tab is pressed).

In case any letter is wrong, the keyboard will reject the letter (will not show) until the right letter is pressed.

Amal.
Community Manager
Community Manager
March 31, 2021

Hi Andreas

 

++ Adding to the correct suggestion by BarlaeDC 

 

For more information about form field properties please check out the help page: https://helpx.adobe.com/acrobat/using/pdf-form-field-properties.html#options_tab_for_form_field_properties

 

Regards

Amal

Inspiring
April 1, 2021

Thanks!

BarlaeDC
Community Expert
Community Expert
March 30, 2021

Hi,

 

For the first plan you could just limit the letters in the properties of the field.

 

The second option is a lot more work.

Inspiring
April 1, 2021

Thank you. I already did that, but I was looking for something more specific.