Handling Single Quotes in Values From ASP

How can I prevent sending the contents of a form field containing single quotes to the server? Single quotes should be converted to double quotes in a way that doesn’t give any problems when writing to the database.


Just make sure that before you pass the data back to the database, you check the string for any improper values. One good way is to create a function called CheckString on your ASP page. This function would convert all occurrences of single quotes to two single quotes that should have no problem in your database. You can then call this function before passing the values to the database.

Function CheckString(byval strText)   ' -- this function converts all single quotes   ' -- inside 'strText' to two single quotes and   ' -- also encloses the entire 'strText' string   ' -- within single quotes   CheckString = "'" & Replace(strText, "'", "''") & "'"End Function

One problem with this code is that it relies on the VBScript Replace function to do the actual replacement. Memory leaks have occurred with the Replace function (hopefully they will be fixed in the future). If you do not wish to rely on the Replace function, you can use this second version of the CheckString function that does a brute force replacement.

Function CheckString(byval strText)   Dim intPos   intPos = InStr(strText, "'")   While intPos > 0      strText = Left(strText, intPos) & _                "'" & _                Mid(strText, intPos + 1)      intPos = Instr(intPos+2, strText, "'")   Wend   CheckString = "'" & strText & "'"End Function

Now, in your ASP page, assuming that you obtained the value of a form field into a variable called strAddress, you would “clean up” the variable before passing it over to the database.

   strAddress = CheckString(strAddress)

Note: If you do not want the CheckString function to enclose the string within single quotes, you can modify the code so it does not do so. Also, if you want single quotes to be replaced by any other character, you can modify the code to do so.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

Top 5 B2B SaaS Marketing Agencies for 2023

In recent years, the software-as-a-service (SaaS) sector has experienced exponential growth as more and more companies choose cloud-based solutions. Any SaaS company hoping to stay ahead of the curve in this quickly changing industry needs to invest in effective marketing. So selecting the best marketing agency can mean the difference

technology leadership

Why the World Needs More Technology Leadership

As a fact, technology has touched every single aspect of our lives. And there are some technology giants in today’s world which have been frequently opined to have a strong influence on recent overall technological influence. Moreover, those tech giants have popular technology leaders leading the companies toward achieving greatness.

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.