Expand my Community achievements bar.

SOLVED

xfa.host.SetFocus question.

Avatar

Former Community Member

 

I have a code on Exit event of the field that checks if 5 digit US ZIP code, 5 digit US ZIP code + 4, and 6 digit alphanumeric Canadian Postal Code.

If user enters valid 5 digits code, then I set focus to specific field-FField1. Code is simple and it works. What does not work is, if I tab out , without entering anyting in the Zip code, the focus is set to some other field, not FField1. How can I force it with the code that focus is set to FField1 all the time.

Code is simple:

var regExpDate = /^\^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]\d$ $/;
var vMatched = this.rawValue.match(regExpDate);
if (!vMatched){
this.rawValue=null;
xfa.host.messageBox("Enter the following format: 99999 or 99999-9999 or A9A 9A9 );
xfa.host.setFocus(this);
}

else
{
xfa.host.setFocus(form1.subform1.FField1);
}

 

 

 

1 Accepted Solution

Avatar

Correct answer by
Level 4

Hi again 1996,

You can use setFocus multiple times in the exit event, and the last one that is called is where you end up. I would suggest putting the 'default' tab before all of your logic, to ensure that it's the one that gets called when the field is valid:

i.e.:

xfa.host.setFocus(form1.subform1.FField1);

var regExpDate = /^\^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]\d$ $/;
var vMatched = this.rawValue.match(regExpDate);
if (!vMatched){
this.rawValue=null;
xfa.host.messageBox("Enter the following format: 99999 or 99999-9999 or A9A 9A9 );
xfa.host.setFocus(this);
}

This way, unless vMatched is true and the second setFocus is called to override the first, your user should always be sent to FField1.

I know that this isn't a part of your question, but I'd like to suggest avoiding the 'thou shalt not pass until...' situations. What if your user doesn't know the postal/zip code, and wants to fill in the rest of the form before finding it? They could enter dummy data to get past your requirement, but then there's a chance that they'll forget to replace it with the real value. Altogether, I think it's better to check for invalid data in the pre-save/pre-submit events, and warn them of the required fields then.

Also, consider only calling setFocus for when the user is tabbing away from fields. If you manually call setFocus to control the tab order from all fields (I do this for all of my forms, as I find that dynamic forms don't play nice with the tab order) then use:

if (xfa.event.commitKey == 3) then

     xfa.host.setFocus("$.parent.NextFieldName");

endif

This way, if the user clicks on another field that they want to fill next, they won't be redirected to the one you chose for them.

Sorry for the unsolicited advice... These were just some of the things that I've had to deal with in the past.

- Scott

View solution in original post

3 Replies

Avatar

Correct answer by
Level 4

Hi again 1996,

You can use setFocus multiple times in the exit event, and the last one that is called is where you end up. I would suggest putting the 'default' tab before all of your logic, to ensure that it's the one that gets called when the field is valid:

i.e.:

xfa.host.setFocus(form1.subform1.FField1);

var regExpDate = /^\^\d{5}-\d{4}|\d{5}|[A-Z]\d[A-Z] \d[A-Z]\d$ $/;
var vMatched = this.rawValue.match(regExpDate);
if (!vMatched){
this.rawValue=null;
xfa.host.messageBox("Enter the following format: 99999 or 99999-9999 or A9A 9A9 );
xfa.host.setFocus(this);
}

This way, unless vMatched is true and the second setFocus is called to override the first, your user should always be sent to FField1.

I know that this isn't a part of your question, but I'd like to suggest avoiding the 'thou shalt not pass until...' situations. What if your user doesn't know the postal/zip code, and wants to fill in the rest of the form before finding it? They could enter dummy data to get past your requirement, but then there's a chance that they'll forget to replace it with the real value. Altogether, I think it's better to check for invalid data in the pre-save/pre-submit events, and warn them of the required fields then.

Also, consider only calling setFocus for when the user is tabbing away from fields. If you manually call setFocus to control the tab order from all fields (I do this for all of my forms, as I find that dynamic forms don't play nice with the tab order) then use:

if (xfa.event.commitKey == 3) then

     xfa.host.setFocus("$.parent.NextFieldName");

endif

This way, if the user clicks on another field that they want to fill next, they won't be redirected to the one you chose for them.

Sorry for the unsolicited advice... These were just some of the things that I've had to deal with in the past.

- Scott

Avatar

Former Community Member

Hello past-tens,

Follwoing code, for some reason, would not work for me. At some point, I had it. Not sure why and still does not work if I have it as a second statment.

if (xfa.event.commitKey == 3) then

     xfa.host.setFocus("$.parent.NextFieldName");

endif

However, 'default' tab before all of logic, works perfectly. I really appreciate your suggestion. As usually you are great help.

Thank you!

Avatar

Level 4

No worries, this forum has helped me out a number of times, and I like to show my appreciation by helping others when I can.

As for the commitKey code not working, I've seen this happen when it's used with javascript instead of formcalc. It's not a huge issue, and converting your validation from javascript to formcalc may not be worth the trouble you would have doing so. For other fields, though, you could try putting the following code in formcalc instead of using javascript, just to see if using formcalc fixes this issue for you:

if (xfa.event.commitKey == 3) then

     xfa.host.setFocus("$.parent.NextFieldName");

endif

My break is ending, though, so I need to sign off. Good luck with your form,

- Scott