Skip to main content
Known Participant
February 28, 2017
Answered

In which field do I enter the JavaScript for charLimit?

  • February 28, 2017
  • 2 replies
  • 1499 views

If you can answer all 5 of my questions, separately, that would really help me.

I have a form that captures information for new employees hired.  In the "First Name" field, I am trying to set the maximum character limit to 20.  I double click the "First Name" field, and go to the "Validate" tab.  I presume this is where you should enter this code.  So I have this.

var f = this.getField("First Name");

f.charLimit = 20;

This is not working.

However, just to test things out, if I try the following code (still under the Validate tab, within the "First Name" field) to set the limit for the Zip Code field to 2 characters, this does work:

var f = this.getField("ZIP");

f.charLimit = 2;

1.  To get the charLimit property to work for the "First Name" field, which field do I have to enter this code in?  Also, which tab?

2.  Why would it be possible to set the character limit of one field from another field (like the Zip code example)?  If I wanted to set the value of a field, shouldn't I be able to type the code under the Validate tab associated with that field?

3.  Consider this part of the code this.getField("First Name");

          1.  Which field does the word this refer to?

          2.  getField( ) is a method of which object?

This topic has been closed for replies.
Correct answer try67

Yes, I did want to do it with Java Script.  I know you can use the GUI to do it like JR_Boulay suggested.  The reason I'm asking is so I can get a better understanding of how JavaScript works.

I get the impression that the script would have to be entered at the document level rather than inside of an individual field.  If this is true, do you know where this code would be entered?


Again, the location for the code depends on when you want it to run... If you only want to run it once you can do it from the console. There's no need to embed it in the actual file.

2 replies

JR Boulay
Community Expert
Community Expert
February 28, 2017

Hi.

Characters limit don't requires JavaScript, just go to Properties : Options : Limit of…

Acrobate du PDF, InDesigner et Photoshopographe
try67
Community Expert
Community Expert
February 28, 2017

1. It's not clear what you mean... The location of the code depends on what event you want to trigger it, and how you want it to work.

2. Yes, it's possible to set the char limit (or the value) of another field in the validation code of a field.

3) a. "this" doesn't refer to any field. In this instance it refers to the currently active Document.

b. The Document object

Known Participant
February 28, 2017

try67,

Regarding my first point, I meant:  when I enter the character limit code, which field do I have to click on first, then under which tab, would I enter the Java Script code?

Regarding your first reply, you mentioned "The location of the code depends on what event you want to trigger it, and how you want it to work."  I wanted to define the "First Name" field with a character limit of 20.  Not sure why I would need an event to make that property take effect.  That would just be the property/setting the field is created with.  I can understand needing an event to trigger a message, but why would I need an event to trigger how many characters a field can accept?  When I open the form, the "First Name" field should have a 20 character max by default.  That is what I was trying to achieve.

Regarding your answer to 3a, I think your answer clears up a lot for me.  If I double clicked the field "First Name" and wrote the code in one of the tabs, I thought the keyword this would refer to the "First Name" field, since the code is "bound" to that field.  But I think your answer will cause me to see things from a different perspective.  Thank you.

Regarding your answer to 3b, I thought that each field had a method called getField( ).  So again, your answer makes sense and I think it clears up a lot for me.

Thank you for answering my questions, by line number.  This makes it easy to follow things.

try67
Community Expert
Community Expert
February 28, 2017

I had assumed you wanted to do it with a script because the char limit value needs to change based on some condition.

If you just want it to be a fixed value use the method described by JR_Boulay above. It's the easiest way of doing it.