View Full Version : Coldfusion Special Characters and Trim Help Please

March 19th, 2011, 12:50 AM
Ok, I am coding a widget for my site which will allow you to "tag" yourself. I have it coded with certain restrictions, and it has a custom error if something is invalid. To finish it off I need to have some way to disallow special characters such as ! @ # in a form input.

I don't want to just replace them, I have a submit button which sends all of the form input data to another file which processes it and then decides if it should give the user and error message or and success message, I will be putting this special characters code in the processing file.

What I want to accomplish
Special Characters in a Form Input -> Submit button is clicked -> The submit button sends the data to the processing file -> The file detects special characters -> The file uses <cflocation> to send the user to the error page

As stated before I don't want to just replace the characters, I would like to detect them, and show the error. Other portion of my site has it but it is encrypted by the person who coded it, and I cannot contact him right now, and he would give some long and confusing answer, I just need a the block of code that I just have to edit a bit.

I would also like to accomplish one more thing.

<cfoutput query="TagGrab">
<cfif TagGrab.recordcount IS 1>
<cfelseif TagGrab.recordcount GT 1>

I have that code, it outputs all of the users tags in a horizontal list, and if there is only 1 tag, it shows it without a comma, but if there is more than one, it separates them with commas, but the way the code is, each tag has a comma after it if there is more than 1, and I like this but that means the final tag in the list has a comma after it too, which I would like to trim, but I can't find anywhere how to trim something from a query like this.

I have a few new questions....

I would like to convert any uppercase text in the form input to lowercase when it is submitted, and I would also like to have it so that if a tag is put in with spaces in front of it like this " tag" or " tag" or "tag " or "tag " I want it to remove all of the spaces in front and behind it so it would put be inserted into our database as "tag" I still want the user to be able to have spaces in the tag, just not at the front or end. Our form input is set to only work up to a maximum of 30 characters, and I don't want users submitting just spaces like " " or " " So is there a way to check if the text just consists spaces and then send the user to an error page I have made, I tried this...

<cfif form.tagupdate is " ">
<cflocation addtoken="false" url="/content/1-Home/settings.cfm?page=tags&success=false5">

But that only works if the text submitted is just one space like this " " that would mean I would have to have 30 of those codes one for each possible amount of spaces like " " and " " and " " and so on... So I need a way to check if the text is just spaces.

June 23rd, 2011, 12:57 AM
First to check if your user input has any content that is invalid.
After the user submits the form

<!--- Check if [email protected]# exist in the string, REFIND will return the string position --->
<cfif REFind("[\!\@\##]", form.text) gt 0>
<!--- if an invalid character is found --->
<cflocation url="index.cfm?fuseaction=page_you_were_on" addToken="no">
<!--- if value is clean insert it into the database --->

Now to address your comma problem. This will come up many times in coldfusion programming. in your situation there are 2 ways to fix it:

Coldfusion has a function called "ValueList". This will pull all of the values from a select column into a list. so you could simply do:

<cfset tagList = ValueList(TagGrab.tag)>

or if its not as simple as a table column you can loop over the values and append them to an array and then convert them to a list:

<cfset tagArray = ArrayNew(1)>
<cfoutput query="TagGrab">
<cfset ArrayAppend(tag)>
<cfoutput>#ArrayToList(tagArray )#</cfoutput>

Lastly for checking if there is space surrounding the tag, rather than to send an error i would just trim it off, coldfusion has a function called trim() that removes any additional spaces around a word so just surround your form data and submit it:


and if you really want to check if there were spaces you can compare the trimmed value to the actual value length and if they dont match then there was a space

<cfif length(trim(form.tagupdate)) is not length(form.tagupdate)>
<!--- There were spaces in your content--->
<cflocation addtoken="false" url="/content/1-Home/settings.cfm?page=tags&success=false5">

I hope that helps.

Scott Steinbeck