cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
WorkHard
Super User
Super User

Best practice to escape user entered comma/semicolon when building a table from a separated value string

In Power Apps. What's the best method to use when creating/concatenating comma-separated values to prevent them from breaking if the user enters a comma in the field that's captured?

 

Say if I have a function like this:

 

 

ClearCollect(  
    InformationCollection,  
    ForAll(Split(Left(BrowseGallery1.Selected.'Multiline Information',Len(BrowseGallery1.Selected.'Multiline Information')-1), "|"),
    	With({_line: Split(Result, ";")},
          {  
           CSerialNumber: Last(FirstN(_line, 1)).Result,
           CUnit: Last(FirstN(_line, 2)).Result,
           CUnitOD: Last(FirstN(_line, 3)).Result,
           CUnitID: Last(FirstN(_line, 4)).Result,  
           CUnitID2: Last(FirstN(_line, 5)).Result,
           CTest: Last(FirstN(_line, 6)).Result
          }  
        )
    )  
)  

 

 

 that expands a string that is separated by a semicolon ";" and a pipe "|".


The above solution will break if a user enters a text with a semicolon or a pipe.

What's the best method to prevent this?

- Do I choose my separator as a very rarely used special character?
- Or maybe I deny the user from entering a pipe or semicolon in those fields?

- Or ideally, some sort of extra logic that would escape the additional semicolons used in those fields by users.

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User
Super User

@WorkHard 

There are a couple ways to handle it.

Usually when we separate with special characters, then when the information is input and before concat of the text with the special characters, I do a substitute for those special characters.

So, if a person enters a | and I plan to use that character, then I first substitute the | in the text with something else special - like a #@VP (vp in this case referring to vertical pipe) very unlikely that the user would intentionally enter #@VP in the text, so, the | is substituted with #@VP and THEN to pull that back, after the text is split, there is a substitute done for any #@VP in the text to |

 

That is one way to do it and allow entry without restriction.  That would be the other way...restrict the user from entering in the | 

 

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

3 REPLIES 3
RandyHayes
Super User
Super User

@WorkHard 

There are a couple ways to handle it.

Usually when we separate with special characters, then when the information is input and before concat of the text with the special characters, I do a substitute for those special characters.

So, if a person enters a | and I plan to use that character, then I first substitute the | in the text with something else special - like a #@VP (vp in this case referring to vertical pipe) very unlikely that the user would intentionally enter #@VP in the text, so, the | is substituted with #@VP and THEN to pull that back, after the text is split, there is a substitute done for any #@VP in the text to |

 

That is one way to do it and allow entry without restriction.  That would be the other way...restrict the user from entering in the | 

 

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Totally forgot about substitute but is something I've used before in other languages.

Thanks a bunch!


Ended up doing this when saving the string:

 

Substitute(Substitute(TextInput2_12.Text,";",":"),"|","/")

 

 

RandyHayes
Super User
Super User

@WorkHard 

I don't know your input, but you might want to consider substituting to something besides ":" or "/" only because those seem like something that would commonly be input.

If you're going to do the substitute "back" to the information originally entered later, that might impact the accuracy.

 

But...glad you got it working!

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (5,564)