Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
| Search | Today's Posts | Mark Forums Read
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 7th, 2004, 03:33 PM
Registered User
 
Join Date: Dec 2003
Location: , , .
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default Want to Strip Chars from a String - Not Working

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
  #2 (permalink)  
Old January 8th, 2004, 06:28 AM
joefawcett's Avatar
Wrox Author
Points: 9,763, Level: 42
Points: 9,763, Level: 42 Points: 9,763, Level: 42 Points: 9,763, Level: 42
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Exeter, , United Kingdom.
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

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:
Code:
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
  #3 (permalink)  
Old January 10th, 2004, 01:08 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

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/cooki...ok.html#source

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Split a string by 80 chars, but not during a word 2BOrNot2B XSLT 4 December 5th, 2008 04:13 AM
query string not rewriting, bookmarks not working rocco50 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 3 February 1st, 2007 01:12 PM
how do I strip a URL down to the filename? mikehsu317 VBScript 1 July 3rd, 2005 02:52 AM
ASP connection string not working codehappy Classic ASP Basics 2 May 18th, 2005 08:43 AM
Query String not working!!!!!!! cmiller Beginning PHP 1 December 16th, 2003 12:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.