Trim strips leading and trailing whitespace from all text fields in content form submissions.
This works for content titles, plain text fields, and usernames, for example.
The module trims prior to form validation, so for instance if you have an integer field and a person entering data fat-fingers a space after a number, he gets a successfully saved node rather than an error message about invalid input. (Note: HTML validation triggers an error for blank spaces in input fields of type email or number before this module can act.)
Because blank spaces are likely trimmed or invisible in rendered HTML, you are most likely to see the corrective effects of this module in alphabetical lists such as from Views. So if the proprietor of We Didn't Think Alphabetical Order Mattered On The Internet tries to jump ahead of Aardvark Agency in your directory, she'll be disappointed.
Note that Drupal already trims whitespace from passwords without warning. Drupal's "Autocomplete (Tags style)" form field widget also trims all surrounding spaces from terms. Trim extends this behavior to all text fields on all content entities. (In the Drupal context, users and taxonomy terms are 'content'.)
Trimming all form entries everywhere was deemed too aggressive (for instance, if you have an options selection where you've made a key a single ' ' for some reason, or want to allow leading or trailing spaces on a field), so we do not trim input on configuration forms.
Trim module is based on exratione's blog post, Drupal: Trim All Form Fields Everywhere. Exratione deserves all credit if this module saves your sanity by sanitizing all stray spaces in your users' input. If it blows up your site, though, file a ticket here.
Note again that it trims values prior to saving, so it is in fact changing what people enter. This seems a reasonable exception to Drupal's rule of not messing with user input and doing all adjustments on output. If you think there should be an option to warn people when their input is trimmed, please contribute to #3388667: Alert people when their text is trimmed.
Caveats
- Trim uses Drupal's Form API, and will not affect anything that bypasses the Form API, such as using the REST API. See #3190001: Trim on presave (rather than validate) so input is treated consistently whether through a form or API (such as feeds)
- HTML input element validation will run before this module can act. Special HTML input field types like number or email will block form submission if there is invalid input, which includes extra spaces in these cases. This is known to be an issue with the e-mail address field on Drupal users. See #3156546: Trim not working on email field of the registration form.
Image credit: WikiMedia Commons
And again, credit to Exratione.
Project information
- Project categories: Content editing experience, Site structure
1,503 sites report using this module
- Created by mlncn on , updated
Stable releases for this project are covered by the security advisory policy.
Look for the shield icon below.

