Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP General > Testing for double apostrophe in a string

Reply
Thread Tools

Testing for double apostrophe in a string

 
 
Les Juby
Guest
Posts: n/a
 
      05-12-2005
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 .

 
Reply With Quote
 
 
 
 
Ray Costanzo [MVP]
Guest
Posts: n/a
 
      05-12-2005
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





"Les Juby" <> wrote in message
news:...
> 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 .
>



 
Reply With Quote
 
 
 
 
Kyle Peterson
Guest
Posts: n/a
 
      05-12-2005
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




"Les Juby" <> wrote in message
news:...
>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 .
>



 
Reply With Quote
 
Kyle Peterson
Guest
Posts: n/a
 
      05-12-2005
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 !"


"Kyle Peterson" <> wrote in message
news:%...
> 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
>
>
>
>
> "Les Juby" <> wrote in message
> news:...
>>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 .
>>

>
>



 
Reply With Quote
 
Kyle Peterson
Guest
Posts: n/a
 
      05-12-2005
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 !"


"Kyle Peterson" <> wrote in message
news:%...
> 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
>
>
>
>
> "Les Juby" <> wrote in message
> news:...
>>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 .
>>

>
>



 
Reply With Quote
 
Chris Hohmann
Guest
Posts: n/a
 
      05-12-2005
"Les Juby" <> wrote in message
news:...
>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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Double apostrophe in SQL Problem MRW ASP .Net 1 12-12-2006 08:35 PM
apostrophe or double quote? Huy Python 9 02-10-2006 01:39 PM
Double Apostrophe " =?Utf-8?B?d3J5dGF0?= ASP .Net 3 05-18-2005 04:51 PM
cannot convert parameter from 'double (double)' to 'double (__cdecl *)(double)' error Sydex C++ 12 02-17-2005 06:30 PM
Re: How to keep apostrophe in string with jscript paul reed ASP .Net 0 10-17-2003 03:31 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57