Very often we are pasting text into field or pasting them via automations.
It’s not very uncommon that a space sneaks in before the text begins and this causes various issues down the pipeline. The same goes extra spaces after the last character. These empty spaces, especially those at the end are not always clearly visible.
It would be helpful to have an additional text input criteria for this: “no leading of trailing spaces”.
Could be quite nice to have a “trim” option which when enabled on the field automatically removes the whitespace from start/end of an input instead of just telling the user to remove it themselves.
The explanation is a little complicated but I’ll try to break it down
You want to write a regex (which is just pattern matching) so that if it fails the patter match, the field validation will fail.
So a passing case, is when the first character is NOT whitespace (\s) and the last character is NOT whitespace.
So, you denote the start with ^
We say [^\s] which basically says - is NOT \s (which is the symbol for whitespace)
The next is .+ where . means match anything and + means as many times as we want. So there can be any character, any number of times AFTER the first character (that’s not a space)
Then, we have [^\s] again - which is to say, the next token, after all the middle bit, shouldn’t be whitespace
Finally, we finish with $ wich means the end of the line/text, which means the previous [^\s] is checking the LAST character.
You can see from my four examples, that only the first line of text is matching, i.e. passing our validation.
In truth, ChatGPT is very good at writing regex like this, so you can always give it a try
That said, I hope this posts helps you understand it a bit more