# How to stop users from entering in multiple spaces before or after words in a text box

**URL:** <https://forum.inductiveautomation.com/t/how-to-stop-users-from-entering-in-multiple-spaces-before-or-after-words-in-a-text-box/41690>\
**Category:** Ignition\
**Created:** [December 21, 2020, 1:20pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-users-from-entering-in-multiple-spaces-before-or-after-words-in-a-text-box/41690 "2020-12-21T13:20:56Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [December 21, 2020, 1:20pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-users-from-entering-in-multiple-spaces-before-or-after-words-in-a-text-box/41690/1 "2020-12-21T13:20:56Z")

</div>

I have a data entry form, on it I have a text box. I check the box to see if it is blank before it gets inserted into the database. But I want to check to see if there is multiple spaces before and after the words to stop users for doing this _example_ “Hydraulic leak jack leaking tilt jack”

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [December 21, 2020, 1:24pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-users-from-entering-in-multiple-spaces-before-or-after-words-in-a-text-box/41690/2 "2020-12-21T13:24:03Z")

</div>

Well I guess Ignition checks for it because it won’t let me type the example the way the user types it

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [December 21, 2020, 1:25pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-users-from-entering-in-multiple-spaces-before-or-after-words-in-a-text-box/41690/3 "2020-12-21T13:25:53Z")

</div>

like this

![text example](https://us1.discourse-cdn.com/inductiveautomation/original/3X/3/5/358d9bf9ad37d7341445f402d8d3f6caea86afde.png)

---

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://forum.inductiveautomation.com/u/peter)\
**Post date:** [December 21, 2020, 1:26pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-users-from-entering-in-multiple-spaces-before-or-after-words-in-a-text-box/41690/4 "2020-12-21T13:26:35Z")

</div>

I’ll start by saying that you can’t stop users from doing anything 🤣

However what you can do is take what the user has entered and strip all leading and trailing whitespace from their data entry, and only then insert data into your Db.

And for multiple spaces between words you can do search/replace (most likely with a regex) to replace multiple occurrences of a whitespace character with a single space.

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [December 21, 2020, 1:27pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-users-from-entering-in-multiple-spaces-before-or-after-words-in-a-text-box/41690/5 "2020-12-21T13:27:52Z")

</div>

I agree about the users, so how do I accomplish that

---

<div class="post-metadata">

**Author:** ![bkarabinchak.psi](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/bkarabinchak.psi/32/83567_2.png) [@bkarabinchak.psi](https://forum.inductiveautomation.com/u/bkarabinchak.psi)\
**Post date:** [December 21, 2020, 1:36pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-users-from-entering-in-multiple-spaces-before-or-after-words-in-a-text-box/41690/6 "2020-12-21T13:36:41Z")

</div>

So on your submit function, you’d do something like (for my example data is a dictionary with all your form data with key names to a named query)

```auto
def submitForm(data):
    data['textInput'] = data['textInput'].strip()
    system.db.runNamedQuery("submitQuery", data)

```

---

<div class="post-metadata">

**Author:** ![peter](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@peter](https://forum.inductiveautomation.com/u/peter)\
**Post date:** [December 21, 2020, 1:40pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-users-from-entering-in-multiple-spaces-before-or-after-words-in-a-text-box/41690/7 "2020-12-21T13:40:51Z")

</div>

Avoiding regex’s you could use the python “strip()” function to remove leading and trailing spaces.

For the whitespace between words, you could use the “split()” function (which without a parameter splits on whitespace), and the “join()” function to recombine things.

Here is some totally untested code (caveat - I don’t write python in my day job):

```auto
trimmed = operatorInput.strip()
cleaned = ' '.join(trimmed.split())

```

As it is an operator input function I don’t expect performance to be an issue, only simplicity, so I’d suggest staying away from regex’s for this one.

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [December 26, 2020, 9:34pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-users-from-entering-in-multiple-spaces-before-or-after-words-in-a-text-box/41690/8 "2020-12-26T21:34:33Z")

</div>

I tested the .strip() function but it only strips the white space before and after, not the extra spacing in between the words. What am I missing?

---

<div class="post-metadata">

**Author:** ![BenKearney](https://avatars.discourse-cdn.com/v4/letter/b/b19c9b/32.png) [@BenKearney](https://forum.inductiveautomation.com/u/BenKearney)\
**Post date:** [December 26, 2020, 9:39pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-users-from-entering-in-multiple-spaces-before-or-after-words-in-a-text-box/41690/9 "2020-12-26T21:39:20Z")

</div>

That’s all .strip() does. The two options suggested to far to deal with the multiple spaces in between were to use .split() or to use regex - i.e. .replace() .

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [December 26, 2020, 10:00pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-users-from-entering-in-multiple-spaces-before-or-after-words-in-a-text-box/41690/10 "2020-12-26T22:00:20Z")

</div>

how do you use replace?

---

<div class="post-metadata">

**Author:** ![BenKearney](https://avatars.discourse-cdn.com/v4/letter/b/b19c9b/32.png) [@BenKearney](https://forum.inductiveautomation.com/u/BenKearney)\
**Post date:** [December 26, 2020, 11:24pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-users-from-entering-in-multiple-spaces-before-or-after-words-in-a-text-box/41690/11 "2020-12-26T23:24:29Z")

</div>

It’s a python function, not an inbuilt Ignition function, so it should be pretty easy to google with “python replace”

---

<div class="post-metadata">

**Author:** ![jshaffer09](https://avatars.discourse-cdn.com/v4/letter/j/df788c/32.png) [@jshaffer09](https://forum.inductiveautomation.com/u/jshaffer09)\
**Post date:** [December 26, 2020, 11:38pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-users-from-entering-in-multiple-spaces-before-or-after-words-in-a-text-box/41690/12 "2020-12-26T23:38:27Z")

</div>

I have, still looking into it

---

<div class="post-metadata">

**Author:** ![BenKearney](https://avatars.discourse-cdn.com/v4/letter/b/b19c9b/32.png) [@BenKearney](https://forum.inductiveautomation.com/u/BenKearney)\
**Post date:** [December 26, 2020, 11:48pm UTC](https://forum.inductiveautomation.com/t/how-to-stop-users-from-entering-in-multiple-spaces-before-or-after-words-in-a-text-box/41690/13 "2020-12-26T23:48:35Z")

</div>

Easiest thing for you to to is to split the string out into a list using the delimiter ’ ’ (space) and then join it back up again. Code is ’ '.join(my\_string.split()) as per peter’s suggestion.
