Response.Write double quotes in JScript

Discussion in 'ASP General' started by Jon Maz, Jul 8, 2004.

  1. Jon Maz

    Jon Maz Guest

    Hi,

    I have written a page to test a function of mine that strips non-English
    accents and various other characters out of a given string. The function
    itself (called 'StripAll' in the code below) now works fine.

    However something else in the test page is not working, and it's starting to
    bug me! If the user types a string containing double quotes into the
    textbox, when the textbox is re-populated after the postback, any part of
    the entered string *after* the first double quote is lost.

    I'm wondering what I'm doing wrong here. It's just a minor point, but it's
    getting annoying!

    Any help appreciated,

    TIA,

    JON

    PS The issue is presumably the RegExp in function
    EscapeDoubleInvertedCommas.



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


    <%@ LANGUAGE="JavaScript"%>


    <%
    var sourceString = "" + Request.Form("txtSourceString");
    var defaultSourceString = "áàäãâåçéèëêíìïîóòöõôøßúùüûýÿ
    ÁÀÄÃÂÅÇÉÈËÊÍÌÏÎÓÒÖÕÔØÚÙÜÛÝY &&&123~~~~45";

    function RemoveDoubleInvertedCommas(sourceString)
    {
    re = new RegExp(("\""), "g");
    return sourceString.replace(re, "\"");
    }

    function EscapeDoubleInvertedCommas(sourceString)
    {
    //trying to refill the textbox on postback with any inverted commas that
    were typed in!
    //NOT WORKING!
    re = new RegExp(("\""), "g");
    return sourceString.replace(re, "\"\"");
    }
    %>




    <form method="post" id="form1" name="form1">

    URL: <input name="txtSourceString" type="text" value="<% if (sourceString
    != "undefined") {Response.Write(EscapeDoubleInvertedCommas(sourceString))}
    else {Response.Write(defaultSourceString)}%>" id="txtSourceString"
    style="width:500px;" />
    <br>
    <input type="submit" name="btnTest" value="Test StripAll" id="btnTest" />
    <br>

    <hr>

    Stripped version: <% if(sourceString != "undefined")
    {Response.Write(StripAll(RemoveDoubleInvertedCommas(sourceString)));}%>
    <br>


    </form>


    <%

    function StripAccents(sourceString)
    {
    re = new RegExp("á|à|ä|ã|â|å", "g");
    sourceString = sourceString.replace(re, "a");

    re = new RegExp("Á|À|Ä|Ã|Â|Å", "g");
    sourceString = sourceString.replace(re, "A");

    re = new RegExp(("ç"), "g");
    sourceString = sourceString.replace(re, "c");

    re = new RegExp(("Ç"), "g");
    sourceString = sourceString.replace(re, "C");

    re = new RegExp(("é|è|ë|ê|´é|´ê"), "g");
    sourceString = sourceString.replace(re, "e");

    re = new RegExp(("É|È|Ë|Ê|´É|´Ê"), "g");
    sourceString = sourceString.replace(re, "E");

    re = new RegExp(("ï|î|í|ì"), "g");
    sourceString = sourceString.replace(re, "i");

    re = new RegExp(("Ï|Î|Í|Ì"), "g");
    sourceString = sourceString.replace(re, "I");

    re = new RegExp(("ñ"), "g");
    sourceString = sourceString.replace(re, "n");

    re = new RegExp(("Ñ"), "g");
    sourceString = sourceString.replace(re, "N");

    re = new RegExp(("ó|ò|ö|õ|ô|ø"), "g");
    sourceString = sourceString.replace(re, "o");

    re = new RegExp(("Ó|Ò|Ö|Õ|Ô|Ø"), "g");
    sourceString = sourceString.replace(re, "O");

    re = new RegExp("ß", "g");
    sourceString = sourceString.replace(re, "ss");

    re = new RegExp(("ú|ù|ü|û"), "g");
    sourceString = sourceString.replace(re, "u");

    re = new RegExp(("Ú|Ù|Ü|Û"), "g");
    sourceString = sourceString.replace(re, "U");

    re = new RegExp(("ý|ÿ"), "g");
    sourceString = sourceString.replace(re, "y");

    re = new RegExp(("Ý|Y"), "g");
    sourceString = sourceString.replace(re, "Y");

    return sourceString;
    }

    function StripAll(sourceString)
    {
    sourceString = StripAccents(sourceString);

    re = new RegExp((" "), "g");
    sourceString = sourceString.replace(re, "");

    re = new RegExp(("_"), "g");
    sourceString = sourceString.replace(re, "");

    re = new RegExp(("&"), "g");
    sourceString = sourceString.replace(re, "");

    re = new RegExp(("~"), "g");
    sourceString = sourceString.replace(re, "");

    re = new RegExp(("'|;|:"), "g");
    sourceString = sourceString.replace(re, "");

    re = new RegExp(("\""), "g");
    sourceString = sourceString.replace(re, "");

    return sourceString;
    }

    %>
     
    Jon Maz, Jul 8, 2004
    #1
    1. Advertising

  2. Jon Maz

    Ray at Guest

    Server.HTMLEncode the value. The problems is that you're ending up with
    something like:

    <input type="text" name="something" value="He said, "Hi how are you?"">

    The browser will see the " before Hi and assume it is the end of the value.
    By Server.HTMLEncoding it, you'll get:

    <input type="text" name="something" value="He said, &quot;Hi how are
    you?&quot;">

    Ray at work

    "Jon Maz" <> wrote in message
    news:...
    > Hi,
    >
    > I have written a page to test a function of mine that strips non-English
    > accents and various other characters out of a given string. The function
    > itself (called 'StripAll' in the code below) now works fine.
    >
    > However something else in the test page is not working, and it's starting
    > to
    > bug me! If the user types a string containing double quotes into the
    > textbox, when the textbox is re-populated after the postback, any part of
    > the entered string *after* the first double quote is lost.
    >
    > I'm wondering what I'm doing wrong here. It's just a minor point, but
    > it's
    > getting annoying!
    >
    > Any help appreciated,
    >
    > TIA,
    >
    > JON
    >
    > PS The issue is presumably the RegExp in function
    > EscapeDoubleInvertedCommas.
    >
    >
    >
    > ------------------------------------------
    >
    >
    > <%@ LANGUAGE="JavaScript"%>
    >
    >
    > <%
    > var sourceString = "" + Request.Form("txtSourceString");
    > var defaultSourceString = "áàäãâåçéèëêíìïîóòöõôøßúùüûýÿ
    > ÁÀÄÃÂÅÇÉÈËÊÍÌÏÎÓÒÖÕÔØÚÙÜÛÝY &&&123~~~~45";
    >
    > function RemoveDoubleInvertedCommas(sourceString)
    > {
    > re = new RegExp(("\""), "g");
    > return sourceString.replace(re, "\"");
    > }
    >
    > function EscapeDoubleInvertedCommas(sourceString)
    > {
    > //trying to refill the textbox on postback with any inverted commas that
    > were typed in!
    > //NOT WORKING!
    > re = new RegExp(("\""), "g");
    > return sourceString.replace(re, "\"\"");
    > }
    > %>
    >
    >
    >
    >
    > <form method="post" id="form1" name="form1">
    >
    > URL: <input name="txtSourceString" type="text" value="<% if (sourceString
    > != "undefined") {Response.Write(EscapeDoubleInvertedCommas(sourceString))}
    > else {Response.Write(defaultSourceString)}%>" id="txtSourceString"
    > style="width:500px;" />
    > <br>
    > <input type="submit" name="btnTest" value="Test StripAll" id="btnTest" />
    > <br>
    >
    > <hr>
    >
    > Stripped version: <% if(sourceString != "undefined")
    > {Response.Write(StripAll(RemoveDoubleInvertedCommas(sourceString)));}%>
    > <br>
    >
    >
    > </form>
    >
    >
    > <%
    >
    > function StripAccents(sourceString)
    > {
    > re = new RegExp("á|à|ä|ã|â|å", "g");
    > sourceString = sourceString.replace(re, "a");
    >
    > re = new RegExp("Á|À|Ä|Ã|Â|Å", "g");
    > sourceString = sourceString.replace(re, "A");
    >
    > re = new RegExp(("ç"), "g");
    > sourceString = sourceString.replace(re, "c");
    >
    > re = new RegExp(("Ç"), "g");
    > sourceString = sourceString.replace(re, "C");
    >
    > re = new RegExp(("é|è|ë|ê|´é|´ê"), "g");
    > sourceString = sourceString.replace(re, "e");
    >
    > re = new RegExp(("É|È|Ë|Ê|´É|´Ê"), "g");
    > sourceString = sourceString.replace(re, "E");
    >
    > re = new RegExp(("ï|î|í|ì"), "g");
    > sourceString = sourceString.replace(re, "i");
    >
    > re = new RegExp(("Ï|Î|Í|Ì"), "g");
    > sourceString = sourceString.replace(re, "I");
    >
    > re = new RegExp(("ñ"), "g");
    > sourceString = sourceString.replace(re, "n");
    >
    > re = new RegExp(("Ñ"), "g");
    > sourceString = sourceString.replace(re, "N");
    >
    > re = new RegExp(("ó|ò|ö|õ|ô|ø"), "g");
    > sourceString = sourceString.replace(re, "o");
    >
    > re = new RegExp(("Ó|Ò|Ö|Õ|Ô|Ø"), "g");
    > sourceString = sourceString.replace(re, "O");
    >
    > re = new RegExp("ß", "g");
    > sourceString = sourceString.replace(re, "ss");
    >
    > re = new RegExp(("ú|ù|ü|û"), "g");
    > sourceString = sourceString.replace(re, "u");
    >
    > re = new RegExp(("Ú|Ù|Ü|Û"), "g");
    > sourceString = sourceString.replace(re, "U");
    >
    > re = new RegExp(("ý|ÿ"), "g");
    > sourceString = sourceString.replace(re, "y");
    >
    > re = new RegExp(("Ý|Y"), "g");
    > sourceString = sourceString.replace(re, "Y");
    >
    > return sourceString;
    > }
    >
    > function StripAll(sourceString)
    > {
    > sourceString = StripAccents(sourceString);
    >
    > re = new RegExp((" "), "g");
    > sourceString = sourceString.replace(re, "");
    >
    > re = new RegExp(("_"), "g");
    > sourceString = sourceString.replace(re, "");
    >
    > re = new RegExp(("&"), "g");
    > sourceString = sourceString.replace(re, "");
    >
    > re = new RegExp(("~"), "g");
    > sourceString = sourceString.replace(re, "");
    >
    > re = new RegExp(("'|;|:"), "g");
    > sourceString = sourceString.replace(re, "");
    >
    > re = new RegExp(("\""), "g");
    > sourceString = sourceString.replace(re, "");
    >
    > return sourceString;
    > }
    >
    > %>
    >
    >
    >
     
    Ray at, Jul 8, 2004
    #2
    1. Advertising

  3. Jon Maz

    Jon Maz Guest

    Bingo! Nice one Ray!

    :)

    JON
     
    Jon Maz, Jul 8, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris White

    Quotes/Double Quotes in Image Control

    Chris White, Sep 22, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    4,917
    Hermit Dave
    Sep 22, 2004
  2. Sydex
    Replies:
    12
    Views:
    6,658
    Victor Bazarov
    Feb 17, 2005
  3. Erland
    Replies:
    4
    Views:
    6,184
    Erland
    Jul 3, 2006
  4. Thomas Henz

    quotes in response.write

    Thomas Henz, Aug 20, 2003, in forum: ASP General
    Replies:
    1
    Views:
    106
    Ken Schaefer
    Aug 20, 2003
  5. jOhn
    Replies:
    1
    Views:
    275
    Phlip
    Jan 29, 2008
Loading...

Share This Page