Question
I have been working with formatted searches for a while now and have come accross a sticky problem. I have several form fields storing this and that about an issue. One of the fields is a text area. The problem occurs when the text area contains line feeds or carriage returns. This didn't show up at first, since the problems is actually only evident when using conditional results with the
SpreadSheetPlugin. The carriage returns in the formfield cause the search syntax to show up on the search page.
I checked the support web to see if I was the only fella having this problem, and hopefully to find a solution. I had seen mention of a solution from time to time, but nothing that was shared with others. I recently delegated the task of fixing this little problem to a coworker. He came up with a pretty simple little javascript so I thought I'd share...
I'm also replacing commas and parenthesis for the same reason.
// Replaces carriage returns with <br> to fix formatted search results
function checkText(Form, Field) {
//Textarea
var urBrowser = navigator.appName;
var oldText = document.forms[Form].elements[Field].value;
if (urBrowser == "Netscape"){
var ripBRText = oldText.replace(/\n/gi, "<br>"); // Replaces carriage returns with <br>
var ripComma = ripBRText.replace(/,/gi, ","); // Replaces commas with ascii code
var ripOpenParen = ripComma.replace(/\(/gi, "("); // Replaces Open Parenthesis with ascii
var ripClosedParen = ripOpenParen.replace(/\)/gi, ")"); // Replaces Closed Parenthesis with ascii
}else if (urBrowser == "Microsoft Internet Explorer"){
var ripBRText = oldText.replace(/\r\n/gi, "<br>");
var ripComma = ripBRText.replace(/,/gi, ",");
var ripOpenParen = ripComma.replace(/\(/gi, "(");
var ripClosedParen = ripOpenParen.replace(/\)/gi, ")");
}
// Returns altered text
document.forms[Form].elements[Field].value = ripClosedParen;
adding the onChange event to call the above code replaces commas parenthesis and carriage returns with ascii codes or
<br>. This may not be the most elegant solution ever, but it has fixed our problem. Hopefully it will be helpful for others as well.
Environment
--
MatthewCardozo - 23 Aug 2006
Answer
If you answer a question - or someone answered one of your questions - please remember to edit the page and set the status to answered. The status selector is below the edit box.