Testing for double apostrophe in a string

L

Les Juby

A client needs a routine to alert him as to which memo records in an
Access-2000 database have had double apostrophes inserted in the text.
These are stopping a Java mouseover from executing.

Normally, while cycling through the recordset, I would use the InStr
function to test for a positive result indicating and embedded text.

ie. If InStr(rstemp("Keywords"),"searched") > 0 Then

would give a true for all <keywords> fields that contained the text
< searched >

But what do you do if you're looking for a double apostrophe itself.?

To specify the string you are searching for you can't use a single
apostrophe to deliniate the string, such as ' " ' and of course
using " " " won't work either.

Is there a character expression maybe that one could use instead.?

Or can someone perhaps suggest shattering my paradigm by using
something other than an If test on a function. Or maybe a different
function.?

thanks ( in anticipation )

. les .
 
R

Ray Costanzo [MVP]

How about passing it through a function to replace the ' with \'?

<%
sValue = "I'm a string"
Function jsEncode(s)
jsEncode = Replace(s, "'", "\'")
End Function
%>

<button onclick="alert('<%=jsEncode(sValue)%>');">Click me</button>


Ray at work
 
K

Kyle Peterson

here is a function I wrote a couple years back.
(probably can be simplified/improved but works fine)

I use the javascript part of it just for the purpose you are talking about

Function FixStr(StrToFix,FixHow)
' This function prepares a string for saving in the database
' And also can prepare a string for editing in a text box
' The reason we need to do this is to stop users from entering HTML or
Javascript
' And also to make sure line breaks get converted to <br> tags

If Not IsNull(StrToFix) and StrToFix <> "" Then

If FixHow = "PrepareForSave" then
StrToFix = replace(StrToFix,"<","&lt;")
StrToFix = replace(StrToFix,">","&gt;")
StrToFix = Replace(StrToFix,vbCr,"<br>")
ElseIf FixHow = "PrepareForTextBox" then
StrToFix = replace(StrToFix,"&lt;","<")
StrToFix = replace(StrToFix,"&gt;",">")
StrToFix = Replace(StrToFix,"<br>",vbCr)
ElseIf FixHow = "PrepareForJavaScript" then
StrToFix = replace(StrToFix,"'","\'")
StrToFix = replace(StrToFix,"""","\'\'")
StrToFix = Replace(StrToFix,vbCrLf,"<br>")
StrToFix = Replace(StrToFix,vbLf,"<br>")
StrToFix = Replace(StrToFix,vbNewLine,"<br>")
End If

End If

FixStr = StrToFix
End function


and you use it like this

FixStr(YourVariable,"PrepareForJavaScript")

I got the idea from an article at
www.powerasp.com
 
K

Kyle Peterson

you have to test for those line breaks too and convert them to "<br>"'s
because if their are any it messes up your javascript as well

probably overkill the way I am testing for line breaks but like I said it
works well under all scenarios I have come up with so far

so like Grandma used to say "**** it !"
 
K

Kyle Peterson

you have to test for those line breaks too and convert them to "<br>"'s
because if their are any it messes up your javascript as well

probably overkill the way I am testing for line breaks but like I said it
works well under all scenarios I have come up with so far

so like Grandma used to say "F@#! it !"
 
C

Chris Hohmann

Les Juby said:
A client needs a routine to alert him as to which memo records in an
Access-2000 database have had double apostrophes inserted in the text.
These are stopping a Java mouseover from executing.

Normally, while cycling through the recordset, I would use the InStr
function to test for a positive result indicating and embedded text.

ie. If InStr(rstemp("Keywords"),"searched") > 0 Then

would give a true for all <keywords> fields that contained the text
< searched >

But what do you do if you're looking for a double apostrophe itself.?

To specify the string you are searching for you can't use a single
apostrophe to deliniate the string, such as ' " ' and of course
using " " " won't work either.

Is there a character expression maybe that one could use instead.?

Or can someone perhaps suggest shattering my paradigm by using
something other than an If test on a function. Or maybe a different
function.?

thanks ( in anticipation )

. les .

If InStr(rstemp("Keywords"),Chr(34)) > 0 Then ...

OR

If InStr(rstemp("Keywords"),"""") > 0 Then ...

Here's an article that discusses embedded quotes:
http://aspfaq.com/show.asp?id=2065
 

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

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top