Response.Write double quotes in JScript

J

Jon Maz

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;
}

%>
 
R

Ray at

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,828
Latest member
LauraCastr

Latest Threads

Top