Expand my Community achievements bar.

Restricting characters and numbers in fields

Avatar

Former Community Member

Hello,

I have a field that I want to allow the user to enter letters, numbers and hyphens, appostrophes, and spaces. Currently I am using the following in the change event of the field:

if (xfa.event.change.match(/[a-z\A-Z\'\-\,]/) == null)
     xfa.event.change = "";

This works fine and only allows letters, appostrophes, hyphens, and spaces.

But when I try to include script to allow numbers as well (see below) it then allows numbers, but will also allow symbols such as #$% etc. I still only want the user to be able to enter appostrophe's hyphens, numbers and letters.

if (xfa.event.change.match(/[a-z\A-Z\'\-\,]/)([0-9]) == null)
     xfa.event.change = "";

Can anyone offer any suggestions please?

Thank you,

NikDK

3 Replies

Avatar

Former Community Member

I would suggest you move the validation to the exit event from the change event. The outcome will be the same. Script attached to the change event for a text field object is fired for each and every character addition, deletion and change. The impact may not be obvious for a small form but this kind of processing can impact performance and memory consumption.

Additionally, I have to say I have never seen or used the syntax 'xfa.event.change.match'.

The following script checks for letters, numbers, apostrophes, hyphens and spaces. The i at the end of the regular expression indicates to ignore case.

// form1.page1.tf::exit - (JavaScript, client)

var str = this.rawValue;

var regExp = /[^a-z0-9 \-\'\"]/i;

if (regExp.test(str)) {

  xfa.host.messageBox("Not OK");

}

else {

  xfa.host.messageBox("OK");

}

Steve

Avatar

Former Community Member

Hi Steve,


Thank you once again for your help.  I have tried this script in the exit event of the field and it is still allowing me to enter characters such a +. When I tab over it then brings up there error message. When I go to submit the form it allows me to submit even though the field has the + in it.

Is there a way to restrict the field so that the user can't even enter symbols other than - ' or spaces? I have done this before with alphabetic fields, where the user is unable to enter numbers whatsoever.

Thanks again,

NikDK

Avatar

Former Community Member

I can take a look. stwalker.adobe@gmail.com

Steve