javascript PDF set focus

Hi All,

I have this code on a PDF, works fine. It will checked if any required fields were not completed and prompt the user to complete them. Problem is I want the set focus to go to the first field that is incomplete and it is not working, can anyone help? Code below

Thanks
Jonathan


{
var emptyFields = [];

for (var i=0; i<this.numFields; i++) {

var f= this.getField(this.getNthFieldName(i));

if (f.type!="button" && f.required ) {

if ((f.type=="text" && f.value=="") || (f.type=="checkbox" && f.value=="Off")) emptyFields.push(f.name);

}

}
////If required fields are not filled in the this app alert will tell them which felids and set focus on the felids section
if (emptyFields.length>0){

app.alert("You must fill in the following fields before you can submit this form:\n\n" + emptyFields.join("\n"));

getField('Name').setFocus();


Jonathan Alberi


7 Answers

Voted Best Answer

The problem is in your last line. getField('Name').setFocus() will set the focus to a field called 'Name' if one exists, NOT the first incomplete field. If 'Name' does not exist it will throw an error which you won't notice because the rest of the code would have executed properly. If you change 'Name' (with quotes) to f.name (no quotes) it will set the focus to one of the empty fields (the last one it found, no the first one).

In my opinion, a more practical approach is to remove the emptyFields array from the code, move the alert inside the loop, set the focus to the first empty field and break the loop as soon as an empty field is found (so as not to have 10 alerts pop up if there are 10 empty required fields).

If for example there are empty required fields, the alert will fire once, name the field, and set the focus to said field. After the user completes the field and clicks the button again, the alert will point out the next empty required field and set the focus to it, and so on until all required fields have been completed. Something like this:

for(var i = this.numFields - 1; i > -1; i--)
{
var fieldName = this.getNthFieldName(i);
if ((
this.getField(fieldName).type!="button" && this.getField(fieldName).required==true) &&
(this.getField(fieldName).value=="" || this.getField(fieldName).value=="Off"))
{
app.alert("You must fill in " +fieldName+ " before you can submit the form.");
this.getField(fieldName).setFocus();
break;
}

}

if(i<0){//your submit code}


By David Dagley   

What exactly is happening when you try to run this script? There are two missing "}" at the end (or add one and remove the one from the first line). You are trying to set the focus to the field named "Name", I assume you want to set it to the first required field without a value. You can do that by using this line:

getField(emptyFields[0]).setFocus();

Karl Heinz Kremer
PDF Acrobatics Without a Net
PDF Software Development, Training and More...
http://www.khkonsulting.com


Karl Heinz Kremer   

Do you have a field called "Name"? If not, it's not going to work.

Do you want to set focus to a specific field, or to one of the "empty fields" list?

.


Visit my custom-made PDF scripts website: http://try67.blogspot.com
Contact me personally: try6767@gmail.com


Gilad D (try67)   

WORKS!!!!! Thank YOU!!!

Now one last thing I noticed, how do I add to the code to check if a radio button yes or no is not checked? I guess adding something here?

if ((f.type=="text" && f.value=="") || (f.type=="checkbox" && f.value=="Off")) emptyFields.push(f.name);


Jonathan Alberi   

My radiobutton(s) are set to Yes1 or No1, but the script does not check for them even though the value should be blank if nothing is entered. Any ideas?


Jonathan Alberi   

No, the value of an unselected Radio button is not blank, it's "Off".


Karl Heinz Kremer   

Thanks again guys! Works!


Jonathan Alberi   


Please specify a reason: