?
Solved

Trapping % sign in forms

Posted on 2006-04-13
16
Medium Priority
?
243 Views
Last Modified: 2013-12-24
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.
0
Comment
Question by:ahillman
  • 9
  • 4
  • 2
  • +1
16 Comments
 
LVL 2

Expert Comment

by:Matt Grofsky
ID: 16449699
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
 

Author Comment

by:ahillman
ID: 16452770
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
 

Author Comment

by:ahillman
ID: 16452843
How about using the Replace string in coldfusion?  I have not used this - can someone example its use?
Thanks!
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:keijz
ID: 16453262
You mean something like this?

<cfset  yourstring = replace(yourstring,'%',' &##37;')>
0
 

Author Comment

by:ahillman
ID: 16453380
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
 
LVL 1

Expert Comment

by:keijz
ID: 16453442
&##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
 
LVL 2

Expert Comment

by:Matt Grofsky
ID: 16453457
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
 

Author Comment

by:ahillman
ID: 16453654
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
 

Author Comment

by:ahillman
ID: 16455667
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
 
LVL 2

Expert Comment

by:Matt Grofsky
ID: 16456103
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
 

Author Comment

by:ahillman
ID: 16456162
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
 
LVL 2

Accepted Solution

by:
Matt Grofsky earned 600 total points
ID: 16456224
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
 

Author Comment

by:ahillman
ID: 16456915
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
 
LVL 16

Expert Comment

by:RCorfman
ID: 16461857
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
 

Author Comment

by:ahillman
ID: 16477139
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
 

Author Comment

by:ahillman
ID: 16478168
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

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
One of the typical problems I have experienced is when you have to move a web server from one hosting site to another. You normally prepare all on the new host, transfer the site, change DNS and cross your fingers hoping all will be ok on new server…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question