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

Filter out carriage returns? 2

Status
Not open for further replies.

Fingerprint

Programmer
Oct 21, 2002
35
GB
Thanks to all the help from people using this forum, my project is now up to the user testing stage, and we've only found one error.

If a user of my form pushes enter/carriage return in a textarea, it messes up the form output - I get the field split into several output fields.
I have wrap set to soft or virtual, but this makes no difference.
I have a simple string filter function, but don't know how to filter out the 'return' character.
Can anybody help please?

- fingerprint
 
Something Like this:

<SCRIPT LANGUAGE=&quot;JavaScript&quot;>
<!--
netscape = &quot;&quot;;
ver = navigator.appVersion; len = ver.length;
for(iln = 0; iln < len; iln++) if (ver.charAt(iln) == &quot;(&quot;) break;
netscape = (ver.charAt(iln+1).toUpperCase() != &quot;C&quot;);

function keyDown(DnEvents) { // handles keypress
// determines whether Netscape or Internet Explorer
k = (netscape) ? DnEvents.which : window.event.keyCode;
if (k == 13) { // enter key pressed
return false;
}
}
}
document.onkeydown = keyDown; // work together to analyze keystrokes
if (netscape) document.captureEvents(Event.KEYDOWN|Event.KEYUP);
// -->
</script> Regards
David Byng
spider.gif

davidbyng@hotmail.com
 
I'm having a hard time following that! The code detects the 'push enter key' event?

It was more a case of removing the character at validation: the form output saves as a '*.csv' file, which is why a carriage return character messes up the output - Excel thinks that the data from that field comes from several fields.

This is my filter procedure (I think I've posted this before for somebody else to use), and I found that putting a special character in as 'strFind' doesn't work.

Code:
function strReplaceAll (str,strFind,strReplace)
 {
  var returnStr = str;
  var start = returnStr.indexOf(strFind);
  while (start>=0)
   {
    returnStr = returnStr.substring(0,start) + strReplace + returnStr.substring(start+strFind.length,returnStr.length);
	start = returnStr.indexOf(strFind,start+strReplace.length);
   }
  return returnStr;
  }

What I was wondering was if there was anything I could use in there for carriage return/line break that would work.

If your code does what I need (I may just be being thick at the moment), how do I apply it?

- fingerprint
 
You just add the code in the page and it will prevent the enter key from being pressed, but if you want to add it to your validation routine then you just need to search and replace: &quot;\n&quot; for newline Regards
David Byng
spider.gif

davidbyng@hotmail.com
 
I've already tried that, and it doesn't work.
Perhaps it's how I'm calling it - see below.

Code:
var textaone
  {
  textaone = strReplaceAll(document.form1.tasks.value,&quot;,&quot;,&quot;&quot;);
  }
 var textatwo
  {
  textatwo = strReplaceAll(textaone,&quot;\n&quot;,&quot;&quot;);
  }
 fieldArray[6] = textatwo;

When I open the output in Notepad, I get little black squares where the returns are, and in any other program, even code capable ones, it actually produces a new line.
Any ideas?
 
This is the replace funtion I use:

Code:
function sReplace (search, replace, string) {
  while (string.indexOf (search) > -1) {
    pos = string.indexOf (search);
    string = (string.substring (0, pos) + replace +
    string.substring ((pos + search.length), string.length));
  }
  return string;
}
Regards
David Byng
spider.gif

davidbyng@hotmail.com
 

var rex = new RegExp(&quot;\r\n&quot;);
var newstr = oldstr.replace(rex,&quot;&quot;);


Didn't actuall test this but it should be very close and at least give you the idea.

-pete
 
This works:

Code:
function sReplace (search, replace, string) {
  while (string.indexOf (search) > -1) {
    pos = string.indexOf (search);
    string = (string.substring (0, pos) + replace +
    string.substring ((pos + search.length), string.length));
  }
  return string;
}


function removeLB (frm,fld) {

var str = document[frm][fld].value;
var newstr = sReplace (&quot;\r&quot;, &quot;&quot;, str);
newstr = sReplace (&quot;\n&quot;, &quot;&quot;, newstr);
alert(newstr);

}

I have tested it Regards
David Byng
spider.gif

davidbyng@hotmail.com
 
Thanks for your help - guys, problem now sorted.
Hopefully, it'll be the last one...

- fingerprint
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top