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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replacing Comma in text field

Status
Not open for further replies.

jmcg

Technical User
Jun 30, 2000
223
GB
I have a javascript function working in Cold Fusion to remove character returns and replace them with <br>.
I have tried to adapt this to remove a comma from a text field before updating (to stop interfering with comma separated list) but it does nothing.
I have tried instead of Chr(44) entering a comma and still nothing. If I use a letter or other ascii it works fine, it just seems to be the comma!
Code:
<script language="JavaScript">
<CFOUTPUT>
function removeCR(checkString) 
{ 
    newString = "";    // REVISED/CORRECTED STRING 
    count = 0;         // COUNTER FOR LOOPING THROUGH STRING 
    // LOOP THROUGH STRING CHARACTER BY CHARACTER 
    for (i = 0; i < checkString.length; i++) { 
        ch = checkString.substring(i, i+1); 
        if ((ch >= "#Chr(44)#") || (ch = "#Chr(32)#")){ 
            newString += ch; 
        } 		
    } 
    return newString; 
} 
</CFOUTPUT>
</script>
 
Looping through strings to remove or replace characters is a very inefficient way to do this. Fortunately netscape provided us with regexp support when they created javascript. The string method replace is the right tool for this job. I'd suggest rewrite all your old scripts to get rid of that string looping process as well for two reasons.

1) it's more efficient and less lines of code using the replace method
2) you get sexy points for using regexp

Observe how your function from above magically become a 1-liner:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

function removeCommas(str) {
   return str.replace(/,/g, "");
}

var a = "this,is,a,string,with,commas";
var b = "this is a string without commas";
var c = ",just checking for commas on the ends,";

alert("before: " + a + "\n\nafter: " + removeCommas(a));
alert("before: " + b + "\n\nafter: " + removeCommas(b));
alert("before: " + c + "\n\nafter: " + removeCommas(c));

</script>
<style type="text/css"></style>
</head>
<body>
</body>
</html>

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Not really a javascript guy so the previous code was an amendment on something found elsewhere.
This works great for the commas, but I cannot seem to get it to work to change the returns using the Chr(44) so still need to run the loop through
 
but I cannot seem to get it to work to change the returns using the Chr(44) so still need to run the loop through

Why are you using cold-fusion escape codes to check the string? You are replacing these client-side, so you should use the javascript escape codes for those instead:

\n - line feed
\r - carriage return

and, if you're looking for a single white space character (this includes spaces, tabs, form feeds, line feeds, and carriage returns), then there's a super handy escape code packaged into one:

\s

Really, using a loop to replace characters is never necessary, it's much more efficient to use the replace method. An example for newlines:
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<script type="text/javascript">

function removeNewLines(str) {
   return str.replace(/\r|\n/g, "<br />");
}

var a = "this is a string\n\n\nwith a bunch of newlines\n\ninserted into the \n\nstring\n\nok!";

alert("before: " + a + "\n\nafter: " + removeNewLines(a));

</script>
<style type="text/css"></style>
</head>
<body>
</body>
</html>

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top