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

Text box field lenght based on combo box

viper6277
Registered: Sep 8 2009
Posts: 6

Hi everyone, I'm new here to the forum, I have a very easy problem by comparison to what I've been reading on the site...but I'm new to java for adobe, so I only have little bits and pieces to go on here..

Basically I'm trying to use 2 combo boxes to help me set the field length of a text box...here's what I have so far...but it's not working

var s = this.getField("state").value; //first combo box, list of states
var c = this.getField("company").value; //second combo box, list of companies
var y = this.getField("account"); //my account number text box.
var x; // the variable that I would use to limit my charter limit.

if (s == "NY" && c == "ABC Company")
{
x = 10;
}
else if (s == "NJ" && c == "DEF Company")
{
x = 14;
}

if (y.length != x)
{
app.alert ("Account number incorrect size");
}

I'm not entirely sure where to put this code, I've tried putting it "validation script" area of the text box...but the alert just seems to come up at random.

I'm thinking that since there are 3 solutions to any problem....maybe I'm not going about it the right way...maybe there is an easier way ????

Any ideas would be much appreciated.

My Product Information:
Acrobat Pro 8.1, Windows
try67
Expert
Registered: Oct 30 2008
Posts: 2398
I think you're a bit confused. A form field has no property called length. Maybe you mean the length of a String, but that is a read only property. You can't set it.
Are you trying to make sure that the input a user has entered is of a certain length?

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

viper6277
Registered: Sep 8 2009
Posts: 6
try67 wrote:
I think you're a bit confused. A form field has no property called length. Maybe you mean the length of a String, but that is a read only property. You can't set it.
Are you trying to make sure that the input a user has entered is of a certain length?
I was looking to do something similar to a social security number format (minus the dashes)...in a text field box...

But the number of characters I'm looking to store in the text box, is dependent on the state that particular company is in.


I looked in the API Reference manual ...and I found "charLimit" ...."Limits the number of characters that a user can type into a text field." ....which sounds like it's part of what I need...but I'm not sure where to put that piece of code...

is it a "Mouse Exit Action"....

is it a "Custom Keystroke Script" .....or

is it a "Custom Validation Script"......

This stuff is all new to me....I've never had a reason to use this much of the PDF file...

Worst part is...I can code this in PHP, in only a few minutes...
try67
Expert
Registered: Oct 30 2008
Posts: 2398
You can put it in the Custom Calculation of the field you want to change.

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

viper6277
Registered: Sep 8 2009
Posts: 6
try67 wrote:
You can put it in the Custom Calculation of the field you want to change.
I tried another iteration of code...putting it in the Custom Keystroke script window...but that didn't work either..

var account = event.value;

if (event.willCommit)
{
var s = this.getField("state");
var c = this.getField("company");

if (s.value == "NY" && c.value == "PEPCO")
{
var x = 9;
}

if (account.length != x);
{
app.alert ("Account number incorrect size");
}
}

This thing is just kicking my butt...
try67
Expert
Registered: Oct 30 2008
Posts: 2398
I wrote "Custom Calculation", not "Custom Keystroke"

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

revoxo
Registered: Jul 20 2009
Posts: 17
This is probably quite crude and I've no doubt someone else will have a far more elegant solution but it seems to work (for me at least):

Set up the "account" field as a comb field (under the "Properties" tab) and give it a default number of characters (say 12). This will immediately suggest to the user that the field requires an entry of fixed size, then:

Put this code in the Custom Keystroke script of the "state" combo box:
//Code start
var myChoice = event.changeEx;
var c = this.getField("company").value;
var a = this.getField("account");

if (myChoice == "Choose...") {
app.alert("Please choose a value from the list!");
}
if (myChoice == "NY" && c == "ABC Co") {
a.charLimit = 10;
}
if (myChoice == "NJ" && c == "DEF Co") {
a.charLimit = 14;
}
//Code end

Put this code in the Custom Keystroke script of the "company" field:
//Code start
var myChoice = event.changeEx;
var s = this.getField("state").value;
var a = this.getField("account");

if (myChoice == "Choose...") {
app.alert("Please choose a value from the list!");
}
if (myChoice == "ABC Co" && s == "NY") {
a.charLimit = 10;
}
if (myChoice == "DEF Co" && s == "NJ") {
a.charLimit = 14;
}
//Code end

Put this code in the Custom Calculation script of the "account" field:
//Code start
var acc = this.getField("account")
var val = event.value;
var max_len = this.getField("account").charLimit;

if (val != "") { //if the field has content
if (val.length != max_len) {
app.alert("The account number is not correct")
acc.setFocus(); //comment out if you don't want the field to hilite
}
}
//Code end

Hope this helps!

One other suggestion:
You could make the "state" field an ordinary text field and have the "company" combo box populate it with the state initials depending on the selection and have the above codes just check for that field alone.