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

Make field required if amount is entered on separate field

HankS
Registered: Apr 17 2009
Posts: 15
Answered

I need to make a field required only if an amount has been entered. For instance I have a reimbursement form and if a user enters an amount into a field such as "Meals for Others" I want them to provide detail about this amount on a separate field on the 2nd page. What is the best way to handle this situation?

My Product Information:
Acrobat Pro 9.1, Windows
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Use the validate event for the first field to set the other fields to either required or not required. How you do this depends on the forms technology. Is this an AcroForm or a LiveCycle form?

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

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

HankS
Registered: Apr 17 2009
Posts: 15
I might add that I'm new to this app and java script. I'm using AcroForm.
Dimitri
Expert
Registered: Nov 1 2005
Posts: 1389
Hi Hanks,

Well you've found the right place here at AUC- welcome! There are some great on demand e-seminars that go over step-by-step creating forms in both Acrobat and LiveCycle Designer as well as some on using JavaScript. In addition the Learning Center has hundreds of articles and tutorials on a huge variety of topics on what you can do with Acrobat. And of course, don't forget to check the Acrobat Help documents too- so many answers to basic questions can be found there. Investing time with the huge amount of resources here will save you time in the long run.

In particular I think the e-seminar Lori Defurio did "Getting Started with Forms" has an order form with features just like you are asking for-

For scripting in Acrobat and PDF there are over 70 tutorials in the Learning Center here and if you want some good basic training check out www.pdfscripting.com which has over 10 hours of training videos and heaps of scripted example PDFs.

Hope this helps,

Dimitri
WindJack Solutions
www.pdfscripting.com
www.windjack.com
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
You can find information on entering scripts into form fields in the tutorials on this web site:
http://www.acrobatusers.com/tutorials/2006/scripts_form_fields

You can find even more information on scripting in acrobat, including some free introduction videos on building forms at www.pdfscripting.com

But here's what do, in the validate script for the first field add code that follows this template:
var rgEmptyTest = /^\s*$/;if(rgEmptyTest.test(event.value)){  // Field is empty, make other fields not requiredthis.getField("MyOtherField").required = false;}else{// Field is not empty, make other fields reqiredthis.getField("MyOtherField").required = false;}

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

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

HankS
Registered: Apr 17 2009
Posts: 15
This code worked. The only change I made was to set the second condition to "true".

Thanks a bunch!
prander
Registered: Nov 6 2008
Posts: 44
Thom:
The form that I'm creating has check boxes that would trigger 'required' fields to be completed. I assume I'm going to use this under "Run JavaScript?" I'm a little greenier than Hank and am not quite sure what I'm replacing for the first and second occurrences of "MyOtherField" in your example. I will actually have multiple required fields (10 or more, like "name" "address" "phoneno" etc.) for my trigger field.
Can you give another examples?

Thanks in advance
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
In a Checkbox you'll want to use the MouseUp trigger and a "Run a JavaScript" Action.

The first thing to do is to determine the state of the check box and then use this to set the required state. In the following code, If the check box is checked then a set of fields are set to "required". If the box is unchecked then those same fields are set to "not required".
var bCkBx = event.target.isBoxChecked(0); this.getField("name").required = bCkBx;this.getField("address").required = bCkBx; ...etc...

The names used in the "this.getField()" function must exactly match the field names. It's best to use short descriptive names that do not contain any spaces or special characters. Just like the names you have listed above. So for being green you're way ahead of the game.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window[/b][/url]

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

HankS
Registered: Apr 17 2009
Posts: 15
I almost have my form complete, but need some direction on the one issue I have.

I have the following code on a "print" push button that I created:

// Validates required fields on PDF form.
// Turns required field off/on based on data in dynamic required fields.
// On Pushing Button Print:

// These are the required fields on the form.
// Populate array with their names.

var requiredFields = new Array(1);
requiredFields[0] = "First Name";
requiredFields[1] = "Last";
requiredFields[2] = "EMPLID";
requiredFields[3] = "Dept";
requiredFields[4] = "Destination";
requiredFields[5] = "Ext";
requiredFields[6] = "Box";
requiredFields[7] = "Purpose";


var alertMsg = new Array(1);
alertMsg[0] = "Please enter first name";
alertMsg[1] = "Please enter last name";
alertMsg[2] = "Please enter your I.D. number";
alertMsg[3] = "Please enter the department";
alertMsg[4] = "Please enter the destination";
alertMsg[5] = "Please enter the phone number";
alertMsg[6] = "Please enter the box number";
alertMsg[7] = "Please enter the purpose of your expense";


var emptyTest = /^\s*$/; //look for any single white space char, one or more times, to the end of the string.

var fieldCount = requiredFields.length
var fld = 0;
var success = true;


for (var i=0; i < fieldCount; i++)
{

fld = this.getField(requiredFields[i]);


if( emptyTest.test(fld.value) ) // if required field is empty

{
var success=false;
app.alert(alertMsg[i]);
fld.setFocus();
break;
}


}


if (success==true) //== //check if all fields are validated


{
this.print(); // prints current form
}


The above code works fine for the hard coded values and prompts for any fields that are not populated. The issue I have is that some fields on the form get set to required (or not required) based on a user action. If I include those fields in the array, the user will get prompted even if the field is not required. I have the following code on validate of a text field. This code sets the field to required if a certain field is populated.

var rgEmptyTest = /^\s*$/;
if(rgEmptyTest.test(event.value))
{ // Field is empty, make other fields not required
this.getField("Name of PersonsRow1").required = false
this.getField("DateRow1").required = false
this.getField("Amount To Line 5Row1").required = false
this.getField("Reason For EntertainmentRow1").required = false;
}
else
{// Field is not empty, make other fields required

this.getField("Name of PersonsRow1").required = true
this.getField("DateRow1").required = true
this.getField("Amount To Line 5Row1").required = true
this.getField("Reason For EntertainmentRow1").required = true;
}


var rgEmptyTest1 = /^\s*$/;
if(rgEmptyTest1.test(event.value))
{

}
else
app.alert("Please enter additional information for meals under the section of Line 5 Requirements on the next page.");
this.pageNum = 2 ;
this.getField("DateRow1").setFocus() ;


I want to also include the fields in the script above (DateRow1, etc) in the same validation script for the push button so that the user gets prompted if the field property is set to required AND the field is not populated . Hope this makes sense.

Thanks in advance
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Include all the fields you want to check in the array of field names, then test for the required property in the loop. You might also consider building up the error message instead of breaking on the first empty field.
var strErr = "";var oFirstBadField = null;for (var i=0; i < fieldCount; i++){fld = this.getField(requiredFields[i]);if( emptyTest.test(fld.value) && fld.required ){var success=false;strErr += alertMsg[i] + "\n";if(!oFirstBadField )oFirstBadField = fld;}} if(!success){app.alert(strErr);oFirstBadField.setFocus();}

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window[/b][/url]

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

HankS
Registered: Apr 17 2009
Posts: 15
Thanks again Thomp. Your last suggestion resolved the issue. Here is the final code for the print button:

// Validates required fields on PDF form.
// Turns required field off/on based on data in dynamic required fields.
// On Pushing Button Print:

// These are the required fields on the form.
// Populate array with their names.

var requiredFields = new Array(1);
requiredFields[0] = "First Name";
requiredFields[1] = "Last";
requiredFields[2] = "EMPLID";
requiredFields[3] = "Dept";
requiredFields[4] = "Destination";
requiredFields[5] = "Ext";
requiredFields[6] = "Box";
requiredFields[7] = "Purpose";
requiredFields[8] = "DateRow1";
requiredFields[9] = "Name of PersonsRow1";
requiredFields[10] = "Reason For EntertainmentRow1";
requiredFields[11] = "Amount To Line 5Row1";
requiredFields[12] = "Text5.0";
requiredFields[13] = "Air miles";



var alertMsg = new Array(1);
alertMsg[0] = "Please enter first name";
alertMsg[1] = "Please enter last name";
alertMsg[2] = "Please enter your I.D. number";
alertMsg[3] = "Please enter the department";
alertMsg[4] = "Please enter the destination";
alertMsg[5] = "Please enter the phone number";
alertMsg[6] = "Please enter the box number";
alertMsg[7] = "Please enter the purpose of your expense";
alertMsg[8] = "Please enter Line 5 Requirement - Date";
alertMsg[9] = "Please enter Line 5 Requirement - Name";
alertMsg[10] = "Please enter Line 5 Requirement - Reason";
alertMsg[11] = "Please enter Line 5 Requirement - Amount";
alertMsg[12] = "Please enter Explanation and Comment";
alertMsg[13] = "Please enter Air Miles";




var emptyTest = /^\s*$/; //look for any single white space char, one or more times, to the end of the string.

var fieldCount = requiredFields.length
var fld = 0;
var success = true;
var strErr = "";
var oFirstBadField = null;




for (var i=0; i < fieldCount; i++)
{fld = this.getField(requiredFields[i]);
if( emptyTest.test(fld.value) && fld.required )
{
var success=false;
strErr += alertMsg[i] + "\n";
if(!oFirstBadField )
oFirstBadField = fld;
break;
}}if(!success)

{
app.alert(strErr);
oFirstBadField.setFocus();
}
else

if (success==true) //== //check if all fields are validated


{
this.print(); // prints current form
}
prander
Registered: Nov 6 2008
Posts: 44
I'm not sure this is working for me.
I was expecting to trigger something like just those required fields to highlight once I picked the scripted option, or when I hit the print button (created via an OK button), it would create an error and say "required."
but nothing.

FYI, the first 5-6 fields are required for any of the three choices
I originally created the form in Pro 8
I'm now using Pro 9


thomp wrote:
In a Checkbox you'll want to use the MouseUp trigger and a "Run a JavaScript" Action.The first thing to do is to determine the state of the check box and then use this to set the required state. In the following code, If the check box is checked then a set of fields are set to "required". If the box is unchecked then those same fields are set to "not required".
var bCkBx = event.target.isBoxChecked(0); this.getField("name").required = bCkBx;this.getField("address").required = bCkBx; ...etc...

The names used in the "this.getField()" function must exactly match the field names. It's best to use short descriptive names that do not contain any spaces or special characters. Just like the names you have listed above. So for being green you're way ahead of the game.
thomp
Expert
Registered: Feb 15 2006
Posts: 4411
Prander,
There are two different issues here. The first one is setting certian fields to required with a check box. This was what you asked for in the first post and the code I provided does this. The second issue is validating those fields before submission or printing, which is handled separately. Hanks has a print button on his form that performs the validtaion before print. You could use a script similar to his for this same purpose.

Thom Parker
The source for PDF Scripting Info
[url=http://www.pdfScripting.com]pdfscripting.com[/url]

The Acrobat JavaScript Reference, Use it Early and Often
[url=http://www.adobe.com/devnet/acrobat/javascript.php]http://www.adobe.com/devnet/acrobat/javascript.php[/url]

Then most important JavaScript Development tool in Acrobat
[url=http://www.acrobatusers.com/tutorials/2006/javascript_console][b]The Console Window[/b][/url]

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

goulet
Registered: Aug 29 2011
Posts: 1
Hi all,

Forgive me, I understand this thread is fairly old, and I am very much a beginner at this stuff. My needs are quite similar to the those of Hank's. I have used his script and modified it to my form fields. What I am realizing is that the scrip only identifies the first unentered required field. It would seem that if a user had left multiple fields empty, they would only have the option of fixing the first one and then have to "re-checking" the form. Is there some sort of solution that allow a user to be led through all of the missing fields. I saw in a different thread something about a "summarized" alert box or error box. Any input would be awesome as I am brand new to using this functionality.

Thanks,

Brendan
try67
Expert
Registered: Oct 30 2008
Posts: 2398
I've developed a generalized function to verify multiple fields. You pass to it an array of fields' names, and it will verify that:
1. Text fields are not empty
2. Combo-boxes and List-boxes' values are not equal to " " (space).
3. Radio-buttons have a selected value

If any of the fields are considered empty, the user will be presented with a messages specifying their names, and the function will return FALSE. Otherwise, it will return TRUE.

Here's the code for this function:
  1. function verifyFields(doc, fields) {
  2.  
  3. var emptyFields = new Array();
  4. for (var i=0; i<fields.length; i++) {
  5. var f = doc.getField(fields[i]);
  6. if (f==null) continue;
  7. if (((f.type=="text" && f.value=="") ||
  8. ((f.type=="combobox" || f.type=="listbox") && f.value==" ") ||
  9. (f.type=="radiobutton" && f.value=="Off")))
  10. emptyFields.push(f.name);
  11. }
  12.  
  13. if (emptyFields.length==0)
  14. return true;
  15.  
  16. var msg = "You must fill-in the following fields:\n";
  17. for (var i=0; i<emptyFields.length; i++) {
  18. msg += emptyFields[i] + ", ";
  19. if (i>0 && i%5==0) msg+="\n";
  20. }
  21. msg = msg.replace(/\,\s+$/,"");
  22.  
  23. app.alert(msg,3);
  24. return false;
  25. }
Here's an example of how it can be used:
  1. if (verifyFields(this, ["First Name", "Last Name", "Address", "Gender"])) this.mailDoc();

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

Bond007AE
Registered: Nov 2 2011
Posts: 6
I tried the above and nothing happens. What is wrong? Age is Textbox, Country is combobox...

0 && i%5==0) msg+="\n";
}
msg = msg.replace(/\,\s+$/,"");

app.alert(msg,3);
return false;
}
>
try67
Expert
Registered: Oct 30 2008
Posts: 2398
If you wish, send me the file and I'll have a look at it.

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