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”
Well I guess Ignition checks for it because it won’t let me type the example the way the user types it
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.
I agree about the users, so how do I accomplish that
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)
def submitForm(data): data['textInput'] = data['textInput'].strip() system.db.runNamedQuery("submitQuery", data)
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):
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.
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?
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() .
how do you use replace?
It’s a python function, not an inbuilt Ignition function, so it should be pretty easy to google with “python replace”
I have, still looking into it
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.