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

Restrict text field from keying certain characters by user

Avatar

Level 1

Dear All,

We have designed dynamic legal forms and created barcodes for the same at the end of page separately. To make user friendly, we need to restrict the form field by keying unwanted characters by the user because the barcode cannot accept certain special characters it shows error.

For example, in name field the user can able to key only alphabets in addition to apostrophe and hyphen. NOT other special characters such as "\[:;<?!~

which is similar to keying alphabetic characters in numeric field.

Thanks,

Rangesh


2 Replies

Avatar

Former Community Member

You can restrict user input by modifying xfa.event.change during the change event.

To remove the characters you mention, try this script:

xfa.event.change = xfa.event.change.replace(/[\"\\[:;<?!~]/g, "");

Note that most often xfa.event.change is a single character when the user is typing, but could be an entire string if the user pastes into the field.

This script handles both cases.

  John

Avatar

Level 2

Hi,
I am trying to restrict the character at position 0 from numeric. That is, a NAME field is not to start with a numeric charater but can have numeric after position0.
I have written this code below in the change event itself of the field, but it doesn seem to work. Could you please suggest as to where i am going wrong? Or any other alternative to restrict numeric characters at position0? Below is the code:

xfa.this.rawValue.charAt(0).event.change = xfa.event.change.replace(/[0-9]/g, "");

Thanks,

Radhika