Our Engineers can Write as well

Restrict multiple spacing in text field in Adobe PDF forms using JavaScript and this is how!

If you ever come across the issue of restricting user entry of multiple spacing in the text field while using Adobe PDF forms, save this blog, as this is handy in explaining how to quickly and effectively solve your issue.

Our technical explanation has the below sections for your easy understanding:

a) A possible scenario

b) The solution

c) Coding

A possible scenario:

Here is a visual representation of how user entry of multiple spaces in a text field can affect the data quality.

javascript validation
The solution with a little description:

We manage to solve this problem by using the Custom Keystroke Script while creating the editable form.

The keystroke event of text fields is called when the user is entering text with the keyboard and while committing the field value. It is also called to validate the default value of a field when set through the UI or by JavaScript and to validate entries provided by autofill.

For the keystroke one needs to check the following:

event.selStart – Property tells the starting position of the current text selection during a keystroke event.

event.change – A string specifies the change in value that the user has just typed. A JavaScript may replace part or all of this string with different characters. The change may take the form of an individual keystroke or a string of characters.

event.rc – Indicates whether a particular event in the event chain should succeed. Set to false to prevent a change from occurring or a value from committing. The default is true.

custom javascripts

// Custom Keystroke for text field

// event.selStart is to check the index of the character string

// event.change is to check the value of the up-coming change in field

//event.rc enables the changes in the text field


if((event.selStart == ” ” ||
event.value[event.selStart-1] == ” ” || event.value[event.selStart] == ” ” )&&
event.change ==” “)
event.rc = false;

javascript editor

We trust that this blog comes to your help when you are trying to restrict multiple spacing in Adobe PDF editable forms.

For similar useful tips, keep checking this space.

ramkumar - blog author

Contributed by:

Software Engineer

Your email address will not be published. Required fields are marked *