Skip to main content
lovewebdev
Inspiring
January 27, 2010
Question

Remove unwanted characters before entering database?

  • January 27, 2010
  • 3 replies
  • 569 views

I usually remove udnesirable special characters that could be entered in a form before submitting to a database with this

<cfset variable = #Replace(variable,"%","", "all")#>

is there a better way I could simply list all of the unwanted values and just replace from that list?

This topic has been closed for replies.

3 replies

Inspiring
February 16, 2010

There's replaceList() or reReplace() which could be used here.

--

Adam

Known Participant
February 15, 2010

I use something like this (below) to not only replace characters, but entire words from strings.   I use mostly use this code to replace 'bad words' in moderated forums with 'alternative text'.

Assume you have a field called 'newdata' for your user to fill out, and they type the following:

"In 99% of the studies, someone always badwords ^ the flow of the discussion"

.

Run this against the 'newdata' field before inserting it into the database (or before displaying it on the screen if you want to show it before it is inserted)

<cfset fixeddata = '#ReplaceList(newdata, "%,^,badword"," percent,up,goodword")#'>

The result, if you output the resulting #fixeddata# would read:

"In 99 percent of the studies, somoene always goodwords up the flow of the discussion"

All kinds of possibilities with this.

Hope it helped,

WHeis

Inspiring
January 28, 2010

The safetext function at cflib.org might help you.