Removing punctuation from a submitted form

Dear all,

I have an application running with SQL Server, it allows users to add data to a database for further processing but if there is a quote mark ", then the form fails at the quote.

I ghave tried the replace mark, now I would like a functon that can be run that checks for set punctuation  and removes it
LVL 1
souldjAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lee W, MVPTechnology and Business Process AdvisorCommented:
I'm not sure I understand - it sounds like you said you tried the replace function... what's wrong with it?

Replace(strText, "'", "''")
alorentzCommented:
Leew answer will work for INSERT statement.

However, it is safest to use the Recordset update instead.  That way you don't need any formatting, and there is much less chance of SQL Injection attacks.

Example:

SQL = "SELECT * from yourtable where id="&request("id")
rs.open sql, conn, 2, 3
rs("fldFirstname") = request("firstname")
rs("fldLastname") = request("lasstname")
'...etc
rs.update

No formatting necessary and almost completely safe.
WMIFCommented:
except this line.  ;)

SQL = "SELECT * from yourtable where id="&request("id")

SQL = "SELECT * from yourtable where id=" & replace(request("id"), "'", "''")
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

alorentzCommented:
>>except this line.  ;)

>>>>almost completely safe.

<grin>
kiddangerCommented:
Please don't ever use the lazy request unless you're accepting input from the URL or the page header for the same variable.  Always use Request.QueryString or Request.Form.

Parameterized queries and stored procedures are much safer than dynamic SQL and recordset input is a performance hit.  You don't need a recordset to perform a SQL update.

Never use selstar.  No reason to make SQL figure it out when you can provide the columns names.

It's easier to test for valid characters rather than invalid ones and react accordingly.  Don't forget about unicode characters when accepting input from the URL.

Server.HTMLEncode should always be used on input variables.
souldjAuthor Commented:
I used the Replace statement and this is what I get

Microsoft VBScript compilation (0x800A03EE)
Expected ')'
/jobs/Nigeria/web 2 SMS pro/sites/ver7 - Nov 2005/site/confirmvoting.asp, line 51, column 48
message = Replace(Request.Form ("message"),""","''")
alorentzCommented:
sould - This is not an answer...just fixing your error as you have it:  Use 4 """"

message = Replace(Request.Form ("message"),"""","''")



timkerCommented:
I would recommend that you not remove punctuation but replace punctuation. Here is how I have done this in the past:

ASP has two functions that can easily convert your text to be database-afe: Escape and Unescape.

When the Escape function encounters a special character (i.e., a character other than a letter or number or one of the characters * + - . / @ or _), it replaces the character with a percent sign (%) and the replaced character's hexadecimal number. So ' becomes %27 and " becomes %22 and , becomes %2C.

Likewise, Unescape will convert the %27 back to ' and %22 to " and %2C back to ,.

Using this you don't lose any of the user input but you protect your database.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
souldjAuthor Commented:
Timker,

How would I apply these functions

This is a line replacng " with ^
...
This looks like the way to go ...

message= Replace(left(request.form("message"),160),"''","^")

SoulDJ
souldjAuthor Commented:
I have looked further int o ym preceding code and found the following.

page 1 :
With the form submites to page 2:
<%
for i=1 to request.form.count%>
<input type=hidden name="<%=Trim(request.form.key(i))%>" value="<%=Replace(Trim(request.form.item(i)),VBCRLF,"") %>">
<%Next%>

The results are as follows if the form has punctuation
<input type=hidden name="message" value="This is test data  for "James" call me!">
You can see from this page, that the data value for message already has the error. the value for data passed form this page will be "This is test data  for ", how do I get this to display ALL the data for the next page.
                                
souldjAuthor Commented:
With further investigation in to the link provided by kiddanger , I used Server.HTMLEncode on the form submission page 2 as described and this has solved the problem.

I therefore allocate the points to kiddanger but thank all for thier inputs!

soulDJ
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP

From novice to tech pro — start learning today.