By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,105 Members | 1,052 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,105 IT Pros & Developers. It's quick & easy.

Response.Write double quotes in JScript

P: n/a
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(sourceS tring))}
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(RemoveDoubleInvertedComma s(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;
}

%>

Jul 19 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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" <jo****@surfeuNOSPAM.de> wrote in message
news:OF**************@TK2MSFTNGP11.phx.gbl...
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(sourceS tring))}
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(RemoveDoubleInvertedComma s(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;
}

%>

Jul 19 '05 #2

P: n/a
Bingo! Nice one Ray!

:-)

JON
Jul 19 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.