Subject: Want to Strip Chars from a String - Not Working
Posted By: WebDevel Post Date: 1/7/2004 2:33:25 PM
Can someone point me in the right direction, please?

I have a multi-page online form.

I save the contents of each page in a cookie, using Name/Value pairs
-- with the equal-sign ("=") and the semi-colon (";") as delimiters.

The above was working fine.

Then I discovered that if the user enters an equal-sign or a semi-colon in a text box or textarea field, my Name/Value pairs no longer work.

So I tried to write a function (stripDelim.js) to be included in each page of the form, which would take the string that the user entered and strip from it any equal-sign or semi-colon characters it encountered, before returning it and adding it to the cookie string.

However, whether I say:
     if ( charAt(strOld[i]) == charAt(strDelim[j]) )
or I say:
     if ( strOld.charAt[i] == strDelim.charAt[j] )
I keep getting an error (in the 1st case) or the value "undefined" when I display these variables in an alert box (in the 2nd case). .

-------------------------------------------

Below is my function:

strDelim = ";=";    // string containing cookie delimiters

function stripDelim(strOld){
/*
input  = string entered by user ("strOld")
output = user string with delimiters stripped from it ("strNew")
if the character at strOld[i] is not a cookie delimiter, then add the character to strNew
when done, return strNew
*/

   var strNew = "";

   for (var i = 0; i < strOld.length; i++)
   {
       for (var j = 0; j < strDelim.length; j++)
       {

//         if ( charAt(strOld[i]) == charAt(strDelim[j]) )
       if ( strOld.charAt[i] == strDelim.charAt[j] )
         {
            // do nothing
         }

         else
         {
            strNew += strOld.charAt[i];
         }

         alert ("strOld.charAt[i] = " + strOld.charAt[i] + " strDelim.charAt[j] = " + strDelim.charAt[j] + " strNew = " + strNew);

       }  // end inner for
   }  // end outer for

   return strNew;

}   // end function

-----------------------------------

Below is how I included the function in the 1st page of my form (N.B., if it matters, I also had another script in the Head, specific to Page 1):

<head>
<title>Multipage Application Form - Page 1</title>
<script type="text/javascript" src="stripDelim.js">
</script>

<script language="JavaScript">
var strForm1 = "";    // global variable

function gotoNext(){
// blah, blah, blah
}    // end function gotoNext()

</head>

---------------------------------------

Below is how I called stripDelim() in my function gotoNext() for Page 1:

var strCookie = "StartForm1;";

   for (var i = 0; i < frmPage1.elements.length - 1; i++)
{
     if (frmPage1.elements[i].type == "text" || frmPage1.elements[i].type == "textarea")
         {
           strCookie += stripDelim(frmPage1.elements[i].value) + ";";
         }
     else
         {
        strCookie += frmPage1.elements[i].value + ";";
         }

   }  // end IF
}  // end For-Loop
Reply By: joefawcett Reply Date: 1/8/2004 5:28:04 AM
I would suggest you use the escape function, or if your target browsers allow it the encodeURIComponent function on the string before you put in the equals sign and semi-colon:

var sName = "Joe=";
var sValue = "Fawcett;";
var sCookie = escape(sName) + "=" + escape(sValue) + ";";
alert(sCookie);

or you could use regular expressions and the replace method.



--

Joe
Reply By: planoie Reply Date: 1/10/2004 12:08:47 PM
Here's a link to a javascript example that uses cookies and seems to handle commas and equal signs in the data.

http://javascript.internet.com/cookies/address-book.html#source

Peter
------------------------------------------------------
Work smarter, not harder.

Go to topic 470

Return to index page 972
Return to index page 971
Return to index page 970
Return to index page 969
Return to index page 968
Return to index page 967
Return to index page 966
Return to index page 965
Return to index page 964
Return to index page 963