Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

using the trim function 3

Status
Not open for further replies.

MarcDePoe

Programmer
Dec 19, 2000
101
0
0
US
I'm not sure why, but when Mac users submit forms on my site, all form field variables on the action page have a bunch of garbage characters (spaces perhaps) appended to them. This is screwing up the "unique-fieldness" of my database.

I can eliminate the problem if I use trim() on every form field, but I was wondering 2 things:

1. Why does this happen?
2. Is there an easier way to trim every form field? (rather than individually trimming each variable)

Thanks,
Marc
 
hi marc

mm, Id suggest javascript validation if you're just trying to eliminate ALL spaces, but trim should be perfect for spaces surrounding the actual user input.

Im not sure what you mean about the mac issue though, you mentioned spaces but said that there are other characters as well. Like what? And maybe do you have an example of some input and the resulting output?

here's the "I dont want no stinking spaces" code. Remember it doesn't like ANY spaces, not just those around the text.

<!--- assumes form has a field called &quot;email&quot; and that you dont want no stinking spaces and that you want to submit the form as soon as its validated

ohh and dont forget to enact this function with <input type=&quot;button&quot; value=&quot;go&quot; onclick=&quot;checkEmail('formname')&quot;>

--->

<script language=&quot;javascript&quot;>
<!--
function checkEmail(name)
{
var doc = eval('document.forms[&quot;' + name + '&quot;]');
var hasspace = doc.email.value.indexOf(&quot; &quot;,0);
if (hasspace > 0)
{
var theS = '';
if(hasspace > { theS = 's'; }
alert('There are ' + hasspace + 'space' + theS + ' in the email field.');
} else {
doc.submit();

}
}
//-->
</script>

let me know if this helped
 
strantheman,

I'm not sure what exactly the problem is, but I thought it may have been some Mac problem that everyone knew about but me.

The odd thing is that I have a hidden field holding the primary key value (not user input), and when the form is submitted and I try displaying the value appended to a filename, it comes up like this ...

File44567
.cfm

Perhaps a cr or lf then a space (shows up as a black square in notepad)

This happens to all the fields, so at the top of each action page I have code like this ...

<cfset form.field1=trim(form.field1)>
<cfset form.field2=trim(form.field2)>
<cfset form.field3=trim(form.field3)>
etc.

I wanted to know if I could somehow replace this repetetiveness with some kind of generic loop.

For instance:

<cfloop
index=&quot;field&quot;
list=&quot;CF'sMagicalListOfAllFormFields&quot;
delimiters=&quot;whatever&quot;
>

<cfset field=trim(field)>

</cfloop>

something like this would require only a simple <cfinclude>, a lot less coding, and a lot less room for overlooked errors.

Is there such a magical way of doing this?

- Marc
 
... also,

Any clue about the Mac problem?
btw ... it happens only with FORM fields

- Marc
 
Hey Marc,

I've heard of unusual behavior from the Mac browsers before. I've run across it on my own projects occasionally but it's never been anything that required investigation so I can't tell you anything definitive unfortunately.

You could always change your <form> method from &quot;post&quot; to &quot;get&quot; so your variables are sent via the url string. This alternate route just might force them to send the variables correctly. This approach does have potential problems such as the length of the URL string needs to be short as earlier versions of IE didn't correctly send url strings over a certain size.

Good luck,
GJ
 
To automate this, try using a loop:

<CFLOOP index=&quot;formItem&quot; list=&quot;#Form.Fieldnames#&quot;>
<CFSET &quot;form.#FormItem#&quot; = trim(&quot;Form.#FormItem#&quot;)>
</cfloop>

This will work unless you only need to trim a select few form fields.

Sempai Ike
 
katsujinken,

Is the variable #Form.Fieldnames# predefined ...
or must I define it?

Thanks,
Marc
 
Hey Marc,

It's a built in variable which contains a comma delimited list of form field names. CF creates it so you don't have to define it.

GJ
 
I thought there should be ... never knew there was!
Life is now that much better

Thanks
Marc
 
Internet Explorer on mac puts a carriage return at the end. Blank file fields it also dumps a 2k garbage file in if it's left empty. Made for a few sleepless nights...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top