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