Trapping % sign in forms

This should be so easy - but I am just not getting my head around it.  

I have a form that people submit - basic.  If a % symbol is used in the form - the form breaks.  What is the code to catch this so when submitted it just gets ignored.  Is it the cftry/cfcatch?

Appreciate the help.
ahillmanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Matt GrofskyCommented:
Where does it break, on submit in the client or on the server?

Well you could cfcatch but you could also just do a replace(Formfield,'%','','ALL') if its on the server thats giving you the problem, and it will just remove the %

If its in the client use a cfinput and set the mask property to something that you only want to allow for instance mask="XXXXXXXXX"

Here are some mask settings I took from Macromedia at http://livedocs.macromedia.com/coldfusion/7/htmldocs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=ColdFusion_Documentation&file=00000279.htm

A mask pattern that controls the character pattern that users can enter, or that the form sends to ColdFusion.

HTML and Flash: For tags with type="text". Mask characters and the corresponding valid input characters are:

A = [A-Za-z]
X = [A-Za-z0-9]
9 = [0-9]
? = Any character
All other characters = insert the literal character
Flash only: For tags with type="datefield". ColdFusion uses the mask pattern to format the selected date. Mask characters are:

D = day; can use 0-2 mask characters.
M = month; can use 0-4 mask characters.
Y = year; can use 0, 2, or 4 characters.
E = day in week; can use 0-4 characters.
For more information, see Masking input data in Usage.
0
ahillmanAuthor Commented:
Thank you Michin for the information.  The problem is on the client side not server, but it is in a textarea and not a cfinput.  It is the comments section of the form.

Code:

<TD valign="top" class="label_cell"><label for="comments"> &nbsp;#ListGetAt(lFormFieldsView,10)#:</label>  </TD>
          <TD valign="top">
                  <TEXTAREA name="comments" id="comments" cols=50 rows=5></TEXTAREA>
                  <cfif listFindNoCase(lRequiredFields,"comments")>*</cfif>
                  <cfif url.ID><hr>#updateLog_value#</cfif>
0
ahillmanAuthor Commented:
How about using the Replace string in coldfusion?  I have not used this - can someone example its use?
Thanks!
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

keijzCommented:
You mean something like this?

<cfset  yourstring = replace(yourstring,'%',' &##37;')>
0
ahillmanAuthor Commented:
yes - so would it be :

<cfset form.comment = replace(form.comment,'%','&##37;')>  ?  what is the &##37; is that the same as char(37)?

where would i place this information on the page?  Can it just go at the top of my form page and live there?
0
keijzCommented:
&##37 is the HTML char entity
(also see http://www.ilovejackdaniels.com/characters_cheat_sheet.png).

you should place the replace-tag above the page handeling the submitted content...
0
Matt GrofskyCommented:
ahillman

<cfset MyFormField = Replace(MyFormField,'%','',ALL>

Adding all at the end of it will replace all occurances of %

What you could also do since you are not using a cfinput, is a little javascript so that all % signs are removed.

<script language="javascript" type="text/javascript">
<!--
function intOnly(i) {
      if(i.value.length>0) {
            i.value = i.value.replace(/[^\d]+/g, '');
      }
}
//-->
</script>

Integer Only Textbox:<br>
<textarea name="MyFormField" cols="" rows="" onChange="intOnly(this);" onKeyUp="intOnly(this);" onKeyPress="intOnly(this);">></textarea>

This one using regular expressions will remove anything that is not a number as an example.
0
ahillmanAuthor Commented:
Well that doesn't seem to be solving the problem.  I have the following in my handeling page and its still saying that my comments are undefined.

<cfset url.comments = replace(url.comments,'%','','ALL')>

If I submit the form without a % symbol it works great otherwise fails with:
Element COMMENTS is undefined in URL. I don't receive this error if submitted without this character.  Also works with other symbols like # etc.

I am using ajax to pass the information upon submittal.  Any thoughts?
0
ahillmanAuthor Commented:
I have raised the points since I believe the difficulty has gone up.  The replace is not going to work.  I believe I will need to go with a regular expression.  I don't have experience with regular expressions.  

So this is what I want to do.

I have the above code with a textarea field that is being submitted through AJAX page which uses the comments.value.  
I want to place code in this AJAX page to replace the % character with (pct).

Here is the code I am working with on the submitt page:

function setCoaching(courseID,classID,comments,startDate,instructorID,liTopics,liStudents)      {
            /*Code to replace % */

                       var commentobj=comments.value;

              /*End code to replace % */

http.open("GET","coachingUpdate.cfm?courseID="+courseID.value+"&classID="+classID.value+"&comments="+comments.value+"&startDate="+startDate.value+"&instructorID="+instructorID.value+"&liTopics="+liTopics.value+"&liStudents="+liStudents.value, true);
http.onreadystatechange = coaching2HandleHttpRes; http.send(null);    
          }

So I use var commentobj = comments.value now I don't know where to go from here.

Can someone help me out with the code to make this happen?

Thanks!
0
Matt GrofskyCommented:
ahillman

This should help you out...go ahead and copy and paste this into a html document and give it a try it will show you how to do a find a replace


<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function replaceChars(entry) {
out = "%"; // replace this
add = "pct"; // with this
temp = "" + entry; // temporary holder

while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add +
temp.substring((pos + out.length), temp.length));
}
document.subform.text.value = temp;
}
//  End -->
</script>
</HEAD>

<!-- STEP TWO: Copy this code into the BODY of your HTML document  -->

<BODY>

<center>
<form name="subform">

<input type=text name=text size=40 value="abcdabcd"><br>
<input type=button name=action value="Done!" onClick="replaceChars(document.subform.text.value);">



</form>
</center>
0
ahillmanAuthor Commented:
Thank you Michin - the only thing is I would like to keep it all in the same function that is already being called.  This way I can keep the code cleaner.

I should be able to do something like this in the exhisting function:

var commentobj=comments.value;
             commentobj.replace("&##37;", "pct");

Then replace the the comment.value with commentobj.  What I have above doesn't work of course but you can see the direction I want to go in.  I just don't know the correct way to state the code.

Really appreciate the effort though!!
0
Matt GrofskyCommented:
ok I see what your saying

how about  (of course you have to reference your variables from the form you have and pass them in an onclick and all that fun stuff)

function setCoaching(courseID,classID,comments,startDate,instructorID,liTopics,liStudents)     {
          /*Code to replace % */

                       var commentobj=comments.value;
                       out = "%"; // replace this
                       add = "pct"; // with this
                       temp = "" + commentobj; // temporary holder

                       while (temp.indexOf(out)>-1)
                       {
                       pos= temp.indexOf(out);
                       temp = "" + (temp.substring(0, pos) + add +
                       temp.substring((pos + out.length), temp.length));
                       }
                        commentobj = temp;

              /*End code to replace % */

http.open("GET","coachingUpdate.cfm?courseID="+courseID.value+"&classID="+classID.value+"&comments="+commentobj+"&startDate="+startDate.value+"&instructorID="+instructorID.value+"&liTopics="+liTopics.value+"&liStudents="+liStudents.value, true);
http.onreadystatechange = coaching2HandleHttpRes; http.send(null);    
         }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ahillmanAuthor Commented:
okay - I think that may have worked.  I need to do further testing so I want to keep the question open for another day.  But if all goes well I will award points.

Thanks!
0
RCorfmanCommented:
I would just use htmlEditFormat(var) on the output. You should do this anyway as it fixes " < >  etc, not just %.... I must be missing something though...
are you saying that if the USER enters % in the field, it causes problems when it is passed BACK to cold fusion, or when you have % in the data, it messes up when it is displayed? I've never had problems with incoming data not working right...
0
ahillmanAuthor Commented:
Well - at first I thought it was working.  It was placing into the db the pct in place of the % sign.  However it only worked sporadically.  Sometimes it worked other times i received the "comments are undefined" error.  I did try the htmlEditFormat and that didn't work, but thanks - still believe that the regular expressions are the way to go.  I believe I will just need to do some more thinking on this problem.  I will keep this open until tomorrow to see if anyone else has any ideas then I will close.

Thanks!
0
ahillmanAuthor Commented:
There were other obstacles that were causing problems.  Once these were removed the code worked the way it should.  Thank you for you assistance!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Servers

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.