Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

REMOVE BLANKS/WHITESPACE Before and after Fillable form field Name/Text entry

Community Beginner ,
Mar 09, 2022 Mar 09, 2022

Hi, I need a quick JavaScript that will take a form field entery (say First_Name) and remove any whitespace or blanks that the end-user might have entered before and after whatever was typed.

 

Also, where to put that script in the field?  There is already some validation going on that is working, but I'm uncertain is this would ba a custom "Action" or custom "Format". 

 

Basically, I want whatever text the user types into the form field to be stripped of whitespace (except for spaces between say the first and middle name) and then left-justified

 

Thanks!

TOPICS
How to , JavaScript , PDF forms
1.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 11, 2022 Mar 11, 2022

Hi,

 

In the validation tab,  add this to your script

event.value = event.value.trim();

 

you could also assign it to a variable if you are doing more work on it

 

var trimmedString = event.value.trim();

// other code goes here

event.value = trimmedString

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Mar 11, 2022 Mar 11, 2022

Yep. That did it. Thanks!   

 

I'm going to start another thread regarding the clear form/reset for button I've implemented.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 11, 2022 Mar 11, 2022
LATEST

Note that this will not work if used in older versions of Acrobat or Reader.

To make it more backwards-compatible you can do it like this:

 

function trim(s) {
if (typeof s != "string") return s;
return s.replace(/^\s+/,"").replace(/\s+$/,"");
}

event.value = trim(event.value);

 

The trim function can be placed as a doc-level script, so you could use it in multiple locations without having to re-declare it each time.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines