• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

Participant ,
Mar 30, 2021 Mar 30, 2021

Copy link to clipboard

Copied

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.

TOPICS
Create PDFs , How to , JavaScript , PDF forms

Views

3.5K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 2 Correct answers

Community Expert , Apr 01, 2021 Apr 01, 2021

In that case this should work for you, you will see how to change word on your own:

if(event.change) {
if(event.value == "")
event.rc = /^[l]+$/.test(event.change);
else if(event.value == "l")
event.rc = /^[e]+$/.test(event.change);
else if(event.value == "le")
event.rc = /^[t]+$/.test(event.change);
else if(event.value == "let")
event.rc = /^[t]+$/.test(event.change);
else if(event.value == "lett")
event.rc = /^[u]+$/.test(event.change);
else if(event.value == "lettu")
event.rc = /^[c]+$/.test(event.change)

...

Votes

Translate

Translate
Participant , Apr 05, 2021 Apr 05, 2021

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

Votes

Translate

Translate
Community Expert ,
Mar 30, 2021 Mar 30, 2021

Copy link to clipboard

Copied

Hi,

 

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

BarlaeDC_0-1617118286855.png

 

The second option is a lot more work.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 01, 2021 Apr 01, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Employee ,
Mar 30, 2021 Mar 30, 2021

Copy link to clipboard

Copied

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_prop...

 

Regards

Amal

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 01, 2021 Apr 01, 2021

Copy link to clipboard

Copied

Thanks!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 30, 2021 Mar 30, 2021

Copy link to clipboard

Copied

+++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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 01, 2021 Apr 01, 2021

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Mar 30, 2021 Mar 30, 2021

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 01, 2021 Apr 01, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 30, 2021 Mar 30, 2021

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 01, 2021 Apr 01, 2021

Copy link to clipboard

Copied

Thanks for you answer.

 

I created textfields that hold a specific answer (string) to a question. Only one answer is right.

I want a script to track the letters a user inputs via keyboard.

In case the user input incorrect, the computer will show the letter.

In case it is wrong, the keyboard will refuse the letter 

and will not show anything until the right letter is chosen.

 

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;

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 01, 2021 Apr 01, 2021

Copy link to clipboard

Copied

Hi,

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 01, 2021 Apr 01, 2021

Copy link to clipboard

Copied

If I got it right in your example you posted  "lettuce" you want that user can only enter "l" then if value is "l" user can only enter "e" ...etc?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 01, 2021 Apr 01, 2021

Copy link to clipboard

Copied

Yes, I changed the word, but you got the idea. The target word (answer) will be tracked at keystroke moment.

So, in case each letter in the sequence is typed correctly, such letter will be displayed.

In case it is not, the keyboard will work but no letter will appear inside the textfield, until another right

letter is typed again.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 01, 2021 Apr 01, 2021

Copy link to clipboard

Copied

In that case this should work for you, you will see how to change word on your own:

if(event.change) {
if(event.value == "")
event.rc = /^[l]+$/.test(event.change);
else if(event.value == "l")
event.rc = /^[e]+$/.test(event.change);
else if(event.value == "le")
event.rc = /^[t]+$/.test(event.change);
else if(event.value == "let")
event.rc = /^[t]+$/.test(event.change);
else if(event.value == "lett")
event.rc = /^[u]+$/.test(event.change);
else if(event.value == "lettu")
event.rc = /^[c]+$/.test(event.change);
else if(event.value == "lettuc")
event.rc = /^[e]+$/.test(event.change);
else event.rc = false;}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 01, 2021 Apr 01, 2021

Copy link to clipboard

Copied

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?

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 01, 2021 Apr 01, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 01, 2021 Apr 01, 2021

Copy link to clipboard

Copied

Thank you very much Ms. Nurani, once again!!

It was really a life-saver.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 31, 2021 Mar 31, 2021

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 01, 2021 Apr 01, 2021

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 31, 2021 Mar 31, 2021

Copy link to clipboard

Copied

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_Acro...

 

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 01, 2021 Apr 01, 2021

Copy link to clipboard

Copied

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;

}

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 01, 2021 Apr 01, 2021

Copy link to clipboard

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Apr 05, 2021 Apr 05, 2021

Copy link to clipboard

Copied

Thanks for your reply.

I eventually received a tip in the community that worked really well for me.

JFYI, my PDF does not use an open-ended kind of answer. I give the users three options for them to choose from.

 

But thank you very much for you guidance and interest to help.

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 05, 2021 Apr 05, 2021

Copy link to clipboard

Copied

Hi,

 

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines