Skip to main content
Inspiring
September 4, 2020
Answered

Sort fields in app.alert by tab order rather than alphabetically

  • September 4, 2020
  • 2 replies
  • 1398 views

Hi there,

I found previously a lot of answers in many of the posts here. I am stuck a bit however and post my first post, hoping for some help from the experts.

The code below works perfectly as required and checks for any 'required' fields which have been missed. The app.alert is listing the fields requiring completion in alphabetical order.

Here is the issue:   The set.Focus() part then takes the user back to the first field requiring completing. This again is in alphabetical order. I would like the user to be taken back to focus on the first field requiring completion based on tab order. Is this possible?

Any help would be greatly appreciated. 

Thanks in advance.

 

//Check for required fields are not empty. IF ok, SaveAs
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 (emptyFields.length>0) {
app.alert("Oops! Please fill in all mandatory fields. \n > following field(s) need to be completed:\n\n" + emptyFields.join("\n"));
getField(emptyFields[0]).setFocus();}

else
app.execMenuItem("SaveAs");

 

This topic has been closed for replies.
Correct answer try67

Replace this part of the code:

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

 

With:

for (var i=0; i<TabOrderFields.length; i++) {
var f= this.getField(TabOrderFields[i]);

2 replies

Bernd Alheit
Community Expert
September 4, 2020

Use a array with the field names in tab order. Then use this array to check the fields.

OroosAuthor
Inspiring
September 4, 2020

Thanks for your replay Bernd,

 

I am a basic user and your suggestion of using an Array sounds great, but is beyond my skills.

I think I could define the array:

var TabOrderFields = new Array(
"SurName",
"FirstName",
"Suburb",
"State",

etc...

)

 

and that is where my skills end. How would I run the array as part of the validation above?

If you have some sample code handy for me to learn more, that would be great. If not, that is fine as it is not a critical requirement.

Thanks again.

 

Bernd Alheit
Community Expert
September 4, 2020

You can use a for loop over the array.

try67
Community Expert
September 4, 2020

Add this line before the alert:

 

emptyFields.sort(function(a, b) {if (a<b) return -1; if (a>b) return 1; return 0; });

OroosAuthor
Inspiring
September 4, 2020

Thank you for the quick replay.

 

I tried to insert the code above, but the sorting in the alert and the setFocus is still alphabetically.

 

 ...if (emptyFields.length>0) {

emptyFields.sort(function(a, b) {if (a<b) return -1; if (a>b) return 1; return 0; });

 app.alert("Oops! Please fill in all mandatory fields. \n  >  following  field(s) need to be completed:\n\n" + emptyFields.join("\n"));

....

Also tried:

 }

emptyFields.sort(function(a, b) {if (a<b) return -1; if (a>b) return 1; return 0; });

 if (emptyFields.length>0) {

app.alert("Oops! Please fill in all mandatory fields. \n  >  following  field(s) need to be completed:\n\n" + emptyFields.join("\n"));

 

try67
Community Expert
September 4, 2020

Sorry, missed the part where you said you wanted it based on tab order... No, that's not possible.

It is possible to sort them based on their page number and even physical place on the page, but that would require a much more complex script.