These forums are now Read Only. If you have an Acrobat question, ask questions and get help from one of our experts.

How to create a character count function? HELP PLEASE ANYONE

dmalloy
Registered: Oct 20 2010
Posts: 21
Answered

I am having a very simular issue but I am trying to count the characters in multiple feilds with a total amount not exceeding 24,000. I am trying to export the data and they gave me a cap on the amout of characters that I can export out of one document. So wanted to do a character count instead of a word count but I can get the coding right here is what I have. Not the greatest at java scrpiting so try not to lol too much. :)
 

function CountLeft(field, count, max)
{
if (field.value.length > max) field.value = field.value.substring(0, max); else
count.value = max - field.value.length;
}

Only 24000 characters allowed!
characters left

My Product Information:
Acrobat Standard 9.0, Windows
dmalloy
Registered: Oct 20 2010
Posts: 21
Here is the most recent code that I have tried and yes of course its still not working.function CountLeft(field, count, max)
{
if (field.value.length > max) field.value = field.value.substring(0, max);
else count.value = max - field.value.length;
}
Only 24000 characters allowed!
characters left
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
You have not provided enough information. We have no idea what you are talking about.

What kind of form is this, LiveCycle or AcroForm? What exactly are the inputs to your function? What is it supposed to do? And what is not working?




Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
You have not provided enough information. We have no idea what you are talking about.

What kind of form is this, LiveCycle or AcroForm? What exactly are the inputs to your function? What is it supposed to do? And what is not working?




Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

dmalloy
Registered: Oct 20 2010
Posts: 21
I am trying to count the characters in multiple feilds with a total amount not exceeding 24,000. I am trying to export the data and they gave me a cap on the amout of characters that I can export out of one document. So wanted to do a character count instead of a word count but I can get the coding right here is what I have. Not the greatest at java scrpit so try not to lol too much.

function cFieldName()
{
function CountCharacters(cFieldName) {
/*
return the number of characters in a field
*/
console.println("field name: " + cFieldName);
// get the field object
var oField = this.getField(cFieldName);
console.println("object: " + oField);
// get the value of the field
var cFieldValue = oField.valueAsString;
console.println(cFieldValue);
//return the number of characters in the field including spaces
return cFieldValue.length;
} // end CountCharacters
}
dmalloy
Registered: Oct 20 2010
Posts: 21
Not sure if this is right but I am maybe putting this in the wrong place. Nothing is happening when I put this inside of the "document Java script" field under advanced. Then I named the script cFieldName. delete everything in the box and place the script in the box and pressed okay and dont get any error messages. Was there something that I did wrong? Do I need to save the file and then reopen the file in order for it to run the script? Or I might be placing the script in the wrong place.
dmalloy
Registered: Oct 20 2010
Posts: 21
this is what I got in the debugger

Acrobat Annotations / Collaboration Built-in Wizard Functions Version 9.0
Acrobat SOAP 9.0


CountCharacters is not defined
1:AcroForm:text1:Keystroke
CountCharacters is not defined
1:AcroForm:text1:Keystroke
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Why are you placing functions within functions?
Like Thom said, it's not clear what you want to achieve. Do you want to prevent a user from entering more than x chars? Do you want to display the number of chars in a text box?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

dmalloy
Registered: Oct 20 2010
Posts: 21
I want to display the number of characters inside a text box. I got the error message when I try the code inside the keystroke script on one of the fields but I need to be able to take a count of all the fields at the same time but I was just trying it there just to see if the code even work for just one.
dmalloy
Registered: Oct 20 2010
Posts: 21
Sorry forgot to erase the box that time and this is whats in the Document JavaScript. Does the coding look correct or did I forget add something not sure if I have everything that I need in there in order for it to work.
function CountCharacters(cFieldName) {
/*
return the number of characters in a field
*/
console.println("field name: " + cFieldName);
// get the field object
var oField = this.getField(cFieldName);
console.println("object: " + oField);
// get the value of the field
var cFieldValue = oField.valueAsString;
console.println(cFieldValue);
//return the number of characters in the field including spaces
return cFieldValue.length;
} // end CountCharacters
try67
Expert
Registered: Oct 30 2008
Posts: 2398
Looks ok, although it's quite a long way to get something as simple as the length of a field's value.
The question is, how are you using it?

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

dmalloy
Registered: Oct 20 2010
Posts: 21
Thats the problem I am not sure where to put the code at and how to make the code run to give me the results back. If you know of a easier way to get this done could you help me out I am not sure how much hair I am going to have after this. I know how to set character limits inside of fields that pretty easy but I dont want to set the length for each field when they might need more in one place and less for another. Thats why I think having a box the would show how many characters that has been type into the whole document after it is open would be the best method. I must say the 24,000 character limit is a major pain in the rear end. I think I am close and going in the right direction but at this point I am lose and dry in the desert without any fuel to keep me going. I am dying over here I need to call in a life line. lol.
try67
Expert
Registered: Oct 30 2008
Posts: 2398
The PM system is not working, it seems, but you can contact me privately at try6767 [at] gmail [dot] com

- AcrobatUsers Community Expert - Contact me personally at try6767 [at] gmail [dot] com
Check out my custom-made scripts website: http://try67.blogspot.com

thomp
Expert
Registered: Feb 15 2006
Posts: 4411
If you want to display the character count of one field, in another field. Then you have a couple of options. You really don't need a document level function.

For example, place this code in the format event of the field that is being counted.

this.getField("CountResult").value = event.value.length;

Where "CountResult" is the name of the field where the count will appear. You can do something similar in the keystroke event that will give you a running count. But unfortunately, using the keystroke event like this would probably compromise performance.

Thom Parker
The source for PDF Scripting Info
www.pdfscripting.com
Very Important - How to Debug Your Script

dmalloy
Registered: Oct 20 2010
Posts: 21
Thanks. Dont forget the offer is always open just let me know.