Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events

focus and clear text field if it validated

Avatar

Level 5

Hi All.

I have TextField with pattern text{AA12345A} to display and the same to validation. If user will enter data with wrong format, for instance, ABA4565B and validation will come up how to clear intered value and keep focus to the same field?

Thanks.

9 Replies

Avatar

Level 10

The pattern text{AA12345A} is not valid itself, it should be text{AA99999A}

Avatar

Level 2

I tried removing all the pattern validations on the field and added this below code in the validate event of the text field and it works..

var strCode;

var f = /^([A-Z]{2}[1-9]{5}[A-Z])$/;

if(this.rawValue != null && String(this.rawValue) != "") {

                    strCode = String(this.rawValue);

                    if (f.test(strCode) == false) {

                                        xfa.host.messageBox("Invalid pattern!");

                                        xfa.host.resetData();

                                        xfa.host.setFocus(this);

                              }

                    }

xfa.host.setFocus(this);

Avatar

Level 5

Hi rashmi. Thanks for replay.

I remove pattern and place your code to validate event like you suggest to do. But unfortunately, when I entered correct or not correct value in both cases I got the error message. How it fix?

Thanks.

Avatar

Level 2

Yes thats right just checked it again..

Is there smething else you are expecting?

Have I missed something?

Avatar

Level 5

But how fix problem? Thanks.

Avatar

Level 10

Hi,

use this script in the exit event of your field.

if (!this.rawValue.match(/^[a-zA-Z]{2}[0-9]{5}[a-zA-Z]{1}$/g)) {

          xfa.host.messageBox("Enter correct format 'AA99999A'.", "Wrong Entry", 0,0);

          this.rawValue = "";

          xfa.host.setFocus(this.somExpression);

}

Avatar

Level 5

HI radzmar. Thanks for replay.

I used your code in EXIT event like you suggested. In preview PDF mode it works without any problem. But when I saved the file and open in Acrobat Pro I met problem. If I type correct value format everything works fine but when I type wrong value I got error message accordingly by code and after click OK button the JavaScript Debugger window pop up with such message:

this.rawValue is null
7:XFA:form1[0]:#subform[0]:txtCIN[0]:exit
TypeError: this.rawValue is null
7:XFA:form1[0]:#subform[0]:txtCIN[0]:exit

How to fix that problem?

Thanks.

Avatar

Level 10

To avoit that exception we need to add one more if-expression to check for null-values.

if (!this.isNull) {

          if (!this.rawValue.match(/^[a-zA-Z]{2}[0-9]{5}[a-zA-Z]{1}$/g)) {

                    xfa.host.messageBox("Enter correct format 'AA99999A'.", "Wrong Entry", 0,0);

                    this.rawValue = "";

                    xfa.host.setFocus(this.somExpression);

          }

}

Avatar

Level 5

Thanks a lot. Just two more questions. For what purpose do you use / g in string format? And if I will need different type format. Where do I read about it?

One more time thanks for help.