9 Replies Latest reply on Aug 3, 2011 5:03 AM by gilly-yo

    Remove spaces and special characters from a form field

    gilly-yo

      Hi,

       

      I am tragically new to all of this, but am trying to create a form in Adobe Acrobat 9.  I am trying to use a Custom Format script to take inputs in a form field and automatically remove spaces and special characters (hyphens specifically).  For example, if a user inputs "RAN-99 06" I would like it to change to "RAN9906."  I found this script that will not let users input special characters

       

      if (!event.willCommit) {

       


          event.change = event.change.replace(/[\$#~%\*\*\^\-\(\)\+=\[\]\{\};\"\<\>\?\|\\\!]/g, "");

      }

      And that's okay, but I can't figure out how to disallow spaces.  Also, the perference would be for a script to allow users to input the data as they like, but to clean it up after they leave the text field.

       

      Thanks in advance!

        • 1. Re: Remove spaces and special characters from a form field
          try67 MVP & Adobe Community Professional

          You want to disallow spaces, or just remove them?

          If the latter, just add a space inside the square brackets of the regular

          expression.

          If the former, it requires a validation script.

          1 person found this helpful
          • 2. Re: Remove spaces and special characters from a form field
            gilly-yo Level 1

            Thanks so much for your assistance.  I have added a space in with the other symbols and now it will not allow them to be typed at all.  This is an acceptable fix, but what is the way so that a user can enter in the data with the spaces, etc, but then they disappear after clicking out of the field?  All the examples I saw for validation scripts popped up a message to alert the user when an invalid character had been entered--is there a way to just change the input directly?  Thanks!

            • 3. Re: Remove spaces and special characters from a form field
              try67 MVP & Adobe Community Professional

              That's what your script does, it removes these characters from the input

              before the value is committed.

              • 4. Re: Remove spaces and special characters from a form field
                gilly-yo Level 1

                Right, but what I mean is...right now the way it's working, the user can't even enter in a space or a hyphen.  The optimal solution would be that the user could enter in the spaces and hyphens, but then, when the user clicks out of the field, the formatting automatically removes them.  So, when the user is able to type (for example) "DFD-44 2" but when the user clicks out of the field, "DFD442" pops in.  Right now, when a user types "DFD-44 2" it doesn't allow the spaces or hyphens to be seen at all.  Thanks and sorry for the confusion!

                • 5. Re: Remove spaces and special characters from a form field
                  gkaiseril MVP & Adobe Community Professional

                  And where exactly have you put your script?

                  • 6. Re: Remove spaces and special characters from a form field
                    maxwyss Level 4

                    From the description, I assume that the script is currently in the Keystroke event. In fact, that would be a most logical way to have it; simply ignore anything unwanted when entered.

                     

                    If you want to allow the user to enter anything, but "clean it up" when done, you would place your code in the Validate event. You will have to adjust your Regular Expression so that it works globally, but that's the whole difference. This will change the value.

                     

                    Note that you can also enter the code into the Format event. However, that would only change the visual representation of the value, but internally, the value would remain as entered.

                     

                    Hope this can help.

                     

                    Max Wyss.

                    1 person found this helpful
                    • 7. Re: Remove spaces and special characters from a form field
                      gilly-yo Level 1

                      I have put the following script

                       

                      if (!event.willCommit) {

                       


                          event.change = event.change.replace(/[\$ #~%\*\*\^\-\(\)\+=\[\]\{\};\"\<\>\?\|\\\!]/g, "");

                      }

                      In the Keystroke event.  I tried to simply paste it into the Validate event, but that doesn't work...what would I need to change for the script to work there?

                       

                      Thanks for all of your help!

                      • 8. Re: Remove spaces and special characters from a form field
                        maxwyss Level 4

                        Yeah, there are some modifications when moving code from the Keystroke event to the Validate event.

                         

                        First, you do not need to test for willCommit. In fact, when the field event sequence reaches the Validate event, the field has committed already. The Validate event does therefore not know the willCommit property.

                         

                        Second, in the Validate event, you work on the event.value and no longer on event.change. So, you will replace accordingly.

                         

                        And that should actually do it.

                         

                        Hope this can help.

                         

                        Max Wyss.

                        • 9. Re: Remove spaces and special characters from a form field
                          gilly-yo Level 1

                          Thanks so much to everyone for all of their help!  Just for reference for others who may be searching for the same thing...I put the following script in as a validation script.  Although the user can input whatever he would like, after clicking out of the field, the script removes spaces and special characters (as defined in the script). 

                           

                          {
                              event.value = event.value.replace(/[\$ #~%\*\*\^\-\(\)\+=\[\]\{\};\"\<\>\?\|\\\!]/g, "");

                          }

                          Thanks again!