trim spaces without ending up in a loop?

trim spaces without ending up in a loop?

If a user enters a space at the beginning or end of the field, I need this space removed.
.trim() will do this, but.... the script
input.value = input.value.trim();
creates a never ending loop.

is there any way to get this done?
Zoho creator doesn't treat a space like a regular character (eg: Creator ignores spaces), so all the tricks I could think of so far fail, like
if (original_value != trimmed_value) { input.form = input.form.trim();} //doesn't work :(
or 
if (value.beginsWith(" ") {}    //doesn't work :(
or
if (value.length() != value.trim().length()) {}    //doesn't work :(