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

Display a message if required fields are blank

kevingrolton
Registered: May 28 2009
Posts: 6
Answered

Hello all,

I've been addicted to this forum while working on these forms, and while I've gotten very close with the other posts here, I still can't get my script to work, and I can't see how to debug it step by step to see where it's going wrong. Maybe you can help me.

I simply want to display a message when the form is printed that will notify the user of any required fields that are blank. I have the following code in the Document Will Print action:

var requiredFields = [];
var j = 0;
 
for (var i = 0; i < this.numFields; i++){ 
 
   if (this.getField(this.getNthFieldName(i)).required){
        requiredFields[j] = this.getField(this.getNthFieldName(i));
        j++;
    }
 
}
 
var error = "Please complete the following fields: ";
 
var msg = false;
 
for (j=0; j < requiredFields.length; j++){
 
    if (requiredFields[j].value == null){
        error = error + requiredFields[j].name;
        msg = true;
    }
 
}
 
if (msg){
    app.alert(error);
    fld.setFocus();
}

When I hit print, absolutely nothing happens. where am I going wrong?

My Product Information:
Acrobat Pro 9.0, Windows
gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
You need to define the array for the required field names, make assignment of missing field names use the field name.

var requiredFields = new Array();// loop through fieldsfor (var i = 0; i < this.numFields; i++){// test for required field and if there is no value for the fieldvar fName =  this.getNthFieldName(i);if (this.getField(fName).required & this.getField(fName).value == ''){// add required field name to array of required field namesrequiredFields[requiredFields.length] = fName;}} var error = "Please complete the following fields: \n\n";for (j=0; j < requiredFields.length; j++){if (requiredFields[j].value == null){error = error + requiredFields[j] + '\n';}} if (requiredFields.length > 0)app.alert(error);

You may also need to adjust your code for special field types like combo boxes, buttons, radio buttons, check boxes, etc.

George Kaiser

kevingrolton
Registered: May 28 2009
Posts: 6
Fantastic! Thanks so much!

I'm trying to make the changes for check boxes and radio buttons, but what is the value of a blank box? I tried zero but that doesn't seem to be it:

var requiredFields = new Array();// loop through fieldsfor (var i = 0; i < this.numFields; i++){// test for required field and if there is no value for the fieldvar fName =  this.getNthFieldName(i);if (this.getField(fName).required & (this.getField(fName).value == '' || this.getField(fName).value == 0)){// add required field name to array of required field namesrequiredFields[requiredFields.length] = fName;}} var error = "Please complete the following fields: \n\n";for (j=0; j < requiredFields.length; j++){if (requiredFields[j].value == null || requiredFields[j].value == 0){error = error + requiredFields[j] + '\n';}} if (requiredFields.length > 0)app.alert(error);
kevingrolton
Registered: May 28 2009
Posts: 6
I got it! I used isBoxChecked and an array of accepted values instead. Thanks for the help!
hina.pdfuser
Registered: Oct 28 2010
Posts: 26
Hello i m totally new in Acrobat. But my requirement is similar. Pls tell me how and where to write javascript code in Acrobat. I google but i cant find Javascript editing window in menu.

Thanks,
Hina

gkaiseril
Online
Expert
Registered: Feb 23 2006
Posts: 4307
What product are you using to create your form?

Acrobat has many places, the fields, the page, bookmarks, document level.

In LiveCycle Designer there is a sizable window under the tool bar.

George Kaiser

hina.pdfuser
Registered: Oct 28 2010
Posts: 26
I am using Acrobat Pro 9.

I have found the way to write javascript. Now when i am trying to execute above mentioned code by you, its doing nothing. I had checkboex and dropdownlists which i have removed for the tie being. There are now only textboxes and a Submit button on which I chose to run javascript on Mouse up. Pls tell what i m missing in following code:
__________________________________________________
var requiredFields = new Array();
for (var i = 0; i < this.numFields; i++)
{
var fName = this.getNthFieldName(i);
if (this.getField(fName).required && (this.getField(fName).value == null ||this.getField(fName).value == ''))
{
requiredFields[requiredFields.length] = fName;
}
}
var error = "Please complete the following fields: \n\n";
for (j=0; j < requiredFields.length; j++)
{
if (requiredFields[j].value == null)
{
error = error + requiredFields[j] + '\n';
}
}

if (requiredFields.length > 0)
{
app.alert(error);
}
__________________________________________________

I can validate one by one also but its very time consuming and adding / removing any field will create errors. That's why i want to implement above code.

Pls help.

Thanks,
Hina

hina.pdfuser
Registered: Oct 28 2010
Posts: 26
while u wake up i have done research and came up with following code:

____________________________________________________________________
var requiredFields = new Array();
for (var i = 0; i < this.numFields; i++)
{
var fName = this.getNthFieldName(i);
var f = this.getField(fName);
if (f.type!="button" && this.getField(fName).required && (this.getField(fName).value == null || this.getField(fName).value == ''))
{
requiredFields[requiredFields.length] = fName;}}
var error = "Please complete the following fields: \n\n";
for (j=0; j < requiredFields.length; j++)
{
if (requiredFields[j].value == null)
{
error = error + requiredFields[j] + '\n';
var f2 = this.getField(requiredFields[j]);
f2.setFocus();
}


}


if (requiredFields.length > 0)
app.alert(error);
____________________________________________________________________


Its working as intended but i dont know how to validate for checkboxes, checkbox list and dropdownlist.
Pls help.

Thanks,
Hina

try67
Expert
Registered: Oct 30 2008
Posts: 2398
For check-boxes, you can check whether or not the value is "Off" (the default value when the box is not checked).
For drop-downs you can simply check the value, just like with text boxes.

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

hina.pdfuser
Registered: Oct 28 2010
Posts: 26
!

Thanks,
Hina

hina.pdfuser
Registered: Oct 28 2010
Posts: 26
Hey guys, the form is now working as intended.

Thanks very much.

AUC forum and their experts rocks !!!

Thanks,
Hina

nalakahe
Registered: Jul 17 2011
Posts: 11
Dear gkaiseril,
i am working with Acrobat Pro X, and a text box for input numbers is available. Another text box is using to convert the previous number into words. it works fine.
my new requirement is to display an automatic message at the second box where the numeric field is with no value.
Regards
Nalaka

NalakaHE