Copy link to clipboard
Copied
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");
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]);
Copy link to clipboard
Copied
Add this line before the alert:
emptyFields.sort(function(a, b) {if (a<b) return -1; if (a>b) return 1; return 0; });
Copy link to clipboard
Copied
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"));
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
Thanks again Try67,
No worries, would just have been a -nice to have'.
Was thinking of 'trapping' the user by validating each field and setFocus back if blank or invalid entry is made. That maybe frustrating for a user.
I guess I could loop through each of the required fields and take the user back to the field that way.
Will do some thinking about it if it becomes an issue.
Thanks again for taking the time to replay.
Copy link to clipboard
Copied
Use a array with the field names in tab order. Then use this array to check the fields.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
You can use a for loop over the array.
Copy link to clipboard
Copied
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]);
Copy link to clipboard
Copied
BIG THANK YOU !
The adjustment to the formula is working perfectly with the array.
Thanks again for your help with this.