Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

restrict special characters

Avatar

Former Community Member
I would like to restrict a field so that only letters, apostrophes’ and hyphens can only be used. (name fields)
Therefore I would like to restrict all other speial characters such as "\[:;<?!~
I have tried the following script in the change event but it is not working:
xfa.event.change = xfa.event.change.replace(/[\"\\[:;<?!~]/g, "");
Can anyone offer any suggestions?
Thanks,
Nik
1 Accepted Solution

Avatar

Correct answer by
Level 10

Try the below in the change event of the control.. Language needs to set to Java Script..

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

Thanks

Srini

View solution in original post

2 Replies

Avatar

Correct answer by
Level 10

Try the below in the change event of the control.. Language needs to set to Java Script..

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

Thanks

Srini

Avatar

Former Community Member

A version that also handles pasting (place it in the field's change event):

xfa.event.change = xfa.event.change.match(/[A-Za-z'-]*/)[0];

Note that your form fillers won't be able to enter digits or non-us chars, such as ü, å, ä ö, ñ.