Results 1 to 2 of 2

Thread: Coldfusion Special Characters and Trim Help Please

  1. #1
    admiralspeedy Guest

    Default Coldfusion Special Characters and Trim Help Please

    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.

    Code:
    				<cfoutput query="TagGrab">
    				<cfif TagGrab.recordcount IS 1>
    					<b>#tag#</b>&nbsp;
    				<cfelseif TagGrab.recordcount GT 1>
    					<b>#tag#</b>,&nbsp;
    				</cfif>
    				</cfoutput>
    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...

    Code:
    <cfif form.tagupdate is " ">
    <cflocation addtoken="false" url="/content/1-Home/settings.cfm?page=tags&success=false5">
    </cfif>
    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.
    Last edited by admiralspeedy; March 19th, 2011 at 12:45 PM.

  2. #2
    uniquetrio2000 Guest

    Smile Hopefully this will answer part of your question

    First to check if your user input has any content that is invalid.
    After the user submits the form

    Code:
    <!--- 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">
    <cfelse>
    <!--- if value is clean insert it into the database --->
    </cfif>
    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:

    Code:
    <cfset tagList = ValueList(TagGrab.tag)>
    <cfoutput>#tagList#</cfoutput>
    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:

    Code:
    <cfset tagArray = ArrayNew(1)>
    <cfoutput query="TagGrab">
       <cfset ArrayAppend(tag)>
    </cfoutput>
    <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:
    Code:
    #trim(form.tagupdate)#
    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

    Code:
    <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">
    </cfif>
    I hope that helps.

    Scott Steinbeck

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •