Remove Leading & Trailing Spaces

We have noticed that some of our data that was imported has unwanted leading and/or trailing spaces which is creating issues when appending new data, general searches etc…

Rather than go to every recorded and manually removed this annoyance, we’re looking for a way to remove (trim) this existing data of the leading and trailing spacing.

Any and all help is appreciated… JON

There are likely three options.

1 - Remove with JavaScript :man_technologist:
(I can’t help you with that :laughing:)
2 - Use Make to trim the text.
This gives the benefit of updating the original data.
3 - Use a Text Formula field to trim (my preferred option)

1 Like

Hey Carl, thank you for the reply…

Removing by using a JS was what I was hoping for.

I thought about using a Text Formula field but due to the fact I have these fields setup already as Short Text and trying to change them, Knack is giving me a warning that I will loose data in I change field type. I know that I can just add new Text Formula field and a simple equation to duplicate the Short Text field with issues, but then I would have to go into all the apps and add the new Text Formula field.

1 Like

Hey @Jon1,
For a one-off exercise, you can export your data to Excel and use the TRIM( ) formula to do this and reimport, otherwise if you want a preventative measure, the below piece of JS should work for you.
It listens for when any inputs (of type text) or text areas loses focus, and trims the text automatically.

texttrim

$(document).on('knack-view-render.any', function(event, view, data) {
  
  $('input[type="text"], textarea').on('blur', function() {
        // Get the trimmed value of the input
        var trimmedValue = $(this).val().trim();
        
        // Update the input with the trimmed value
        $(this).val(trimmedValue);
    });

});
2 Likes

Hey Stephen, thanks for the code. Works like a charm.

It actually helped me with another issue that I posted a couple days ago with removing a user’s “extra” space in search field.

I will posted solution in that post and of course give credit to you.

Thanks again… JON

1 Like

Awesome @StephenChapman - that’s gone into my code snippet library and will form part of my setup on apps moving forward.
Leading and trailing spaces can be a pain to deal with.
A super simple code based solution. :man_technologist:
Thank you :pray:

1 Like

Thanks @Jon1 and @CarlHolmes!
I’ve mentioned how to adjust the code to handle pressing enter in search inputs here.