# Remove Leading & Trailing Spaces

**URL:** <https://forums.knack.com/t/remove-leading-trailing-spaces/14960>\
**Category:** Ask the Community\
**Created:** [October 4, 2023, 10:52am UTC](https://forums.knack.com/t/remove-leading-trailing-spaces/14960 "2023-10-04T10:52:46Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jon1](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jon1](https://forums.knack.com/u/Jon1)\
**Post date:** [October 4, 2023, 10:52am UTC](https://forums.knack.com/t/remove-leading-trailing-spaces/14960/1 "2023-10-04T10:52:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CarlHolmes](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/carlholmes/32/2814_2.png) [@CarlHolmes](https://forums.knack.com/u/CarlHolmes)\
**Post date:** [October 4, 2023, 11:28am UTC](https://forums.knack.com/t/remove-leading-trailing-spaces/14960/2 "2023-10-04T11:28:27Z")

</div>

There are likely three options.

1 - Remove with JavaScript 👨‍💻  
(I can’t help you with that 😆)  
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)

> **[Text Formula Functions](https://learn.knack.com/article/z2uiemxq8t-text-formula-functions)**
>
> Everything you need to know about text formulas and how to use them in your Knack app.

---

<div class="post-metadata">

**Author:** ![Jon1](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jon1](https://forums.knack.com/u/Jon1)\
**Post date:** [October 4, 2023, 11:49am UTC](https://forums.knack.com/t/remove-leading-trailing-spaces/14960/3 "2023-10-04T11:49:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![StephenChapman](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/stephenchapman/32/1759_2.png) [@StephenChapman](https://forums.knack.com/u/StephenChapman)\
**Post date:** [October 5, 2023, 1:57am UTC](https://forums.knack.com/t/remove-leading-trailing-spaces/14960/4 "2023-10-05T01:57:40Z")

</div>

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](https://us1.discourse-cdn.com/flex020/uploads/knack/original/2X/b/b7fb762ec00b15b17b302fe2e2253313bc91f71a.gif)

```auto
$(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);
    });

});

```

---

<div class="post-metadata">

**Author:** ![Jon1](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@Jon1](https://forums.knack.com/u/Jon1)\
**Post date:** [October 5, 2023, 11:35am UTC](https://forums.knack.com/t/remove-leading-trailing-spaces/14960/5 "2023-10-05T11:35:47Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![CarlHolmes](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/carlholmes/32/2814_2.png) [@CarlHolmes](https://forums.knack.com/u/CarlHolmes)\
**Post date:** [October 5, 2023, 12:03pm UTC](https://forums.knack.com/t/remove-leading-trailing-spaces/14960/6 "2023-10-05T12:03:53Z")

</div>

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. 👨‍💻  
Thank you 🙏

---

<div class="post-metadata">

**Author:** ![StephenChapman](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/stephenchapman/32/1759_2.png) [@StephenChapman](https://forums.knack.com/u/StephenChapman)\
**Post date:** [October 5, 2023, 8:25pm UTC](https://forums.knack.com/t/remove-leading-trailing-spaces/14960/7 "2023-10-05T20:25:54Z")

</div>

Thanks @Jon1 and @CarlHolmes!  
I’ve mentioned how to adjust the code to handle pressing enter in search inputs [here](https://forums.knack.com/t/trim-search-input/14952/4).
