Prevent spaces in text field

Prevent spaces at the beginning and end of text fields

A common habit when filling out text fields is for a high proportion of users to fill in their place of residence with ending (often) or beginning (rarely) spaces, for example. If you then edit the CSV export in MS Excel and, for example, combine the zip code and city, the unnecessary spaces make the result difficult to read.

Custom validation

With custom validation it is possible to prevent these leading and trailing spaces.

The setting for custom validation is in the field configuration, tab ‘Basic Settings’ in the ‘Custom Validation’ option.
Custom validation is described in the documentation in the following section: Validation of inputs.

Prevent text entries with regex expressions

The following two alternative regex expressions prevent text entries with spaces or tabs at the end.

[^\s]+$

[\S]+$

Explanation:

  • \s
    This expression stands for whitespace, i.e. spaces and tabs.
  • \S
    This expression stands for all characters that are not whitespace, i.e. for [^s].
  • ^
    This character at the beginning of a character selection negates it.
  • +
    This symbol stands for “must occur at least once”.
  • $
    This character represents the end of the text.