Looking for best practices on web.

In a general sense, we have an input on our webpage. This input doesn't allow empty space at the end.

Bad: Apples    

Good: Apples

So what I'm wondering is, how best to handle this.


  1. Add error message.
  2. Trim it on the client side, ie: onBlur.
  3. Trim it on the server side.

Any other options?

We're looking for the general best practices. We could have multiple errors that occur, and the user may not expect the changes, or the corresponding input may dictate the rest of a longer workflow.

2 Answers 2


Long story (skip to TL;DR: if painful)

We have been wrestling with this problem as well on specific type of input data.

Our customers create user accounts to their users in our web application, and some of them "cut and paste" the username (and email address and phone number) from emails or Word documents to the web form.

At one point a customer contacted our helpdesk and said that the user can't log in, but the customer can.

The reason why the customer could log in was that she cut and pasted the "username " also to the login form, and the user typed the given "username". So the user account was unintentionally created with a trailing whitespace in the end of the username.

The first time that happened we just trimmed the whitespace from the database, but after few more of these we set trimming for the user account creation on the server side. Because no-one intentionally wants a trailing whitespaces in the username (or email or phone number), and our customers seem to do a lot of copy-pasting with that type of information.


Copy-pasting data and smart phone keyboards very often unintentionally add trailing spaces after words in input fields.

Depending on the type of input:

  1. if it's 100% sure no-one intentionally wants trailing whitespaces on the input data trim them automatically (usernames, email addresses, phone numbers).

  2. if it's not that clear (free form text inputs) then give some type of error (and maybe provide an action there to trim the input(s) for the user)


Giving a user an error message for something they didn't do wrong is always considered bad in my opinion. Preferably make the system take any spaces at the end away automatically without the user noticing it. It's up to the developers to see how they can best do this.

  • Yup. Similar to handing things like dates etc. If your back-end is expecting 01-08-19 and the user enters 1\8\2019 they haven't entered a wrong date. You should convert what they've entered into the format you need. Put the effort onto the development, not onto each individual user.
    – JonW
    Commented Feb 20, 2019 at 9:43
  • Agree with the sentiment; slight note of caution: make sure any "silent cleaning" process can't result in something other than the user was reasonably intending. Slightly contrived example: suppose someone pasted abcde....xyz (where the .s represent spaces) into a field that can hold a maximum of eight characters and must not have trailing spaces. Were you to silently strip trailing spaces and ignore characters beyond the maximum, they would end up with abcde with no indication that their input had been changed.
    – TripeHound
    Commented Feb 20, 2019 at 14:15
  • You mentioned how best to do this. What would be the ideal solution? Should we clean it up when the user loses focus of that element (onBlur) or should we clean it up on API call? Commented Feb 20, 2019 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.