[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


how to check for apostrophe in a string

Posted on 2012-08-20
Medium Priority
Last Modified: 2012-08-20
I have a text box that will sometimes have a string with an apostrophe.  Before the string is sent to javascript I need to change the ' with a \'.   How can I accomplish this.

Question by:mgmhicks
LVL 83

Accepted Solution

CodeCruiser earned 2000 total points
ID: 38312575
Just use .replace method

Dim str as string = "your's truly"
str = str.Replace("'", "\'")
LVL 31

Expert Comment

ID: 38312590
There's simple ways you can escape the string, for example:


However these functions have some limitations and may not necessarily escape everything.

A more generic way of passing data from PHP to Javascript is to use JSON Encoding, which encodes a data value in string format such that it can be decoded by Javascript. It's much more flexible - you can encode strings, numbers, objects w/ properties, even arrays of values.

Encode in PHP using json_encode()

And decode in Javascript using jQuery's parseJSON() function
LVL 16

Expert Comment

ID: 38312623
On the assumption you are using an AJAX call to populate the form field, the safest method to ensure that all control characters that might affect Javascript are handled you should use the PHP functions "htmlspecialchars()" and "htmlentities()".

This will handle not only a single quote but also address any XSS (cross site scripting) style attempts. The text will be displayed in human readable form within the TEXTAREA field.

Author Comment

ID: 38312653
here is the code its erroring on.  Can I use a javascript function to change the string if necessary.  I dont really want to do it in vb.

function ShowHide(myChoice) {

                      if (myChoice == "1") {
                            $('input[id$=txtFName]').val('<%=session("FName") %>');
                            $('input[id$=txtLName]').val('<%=session("LName") %>');
                            $('input[id$=txtAddr1]').val('<%= session("Address") %>');
                            $('input[id$=txtCity]').val('<%= session("City") %>');
                            //$('input[id$=txtState]').val('<%= session("State") %>');
                            $('input[id$=txtZipCode]').val('<%= session("ZipCode") %>');
                            $('input[id$=txtEmail]').val('<%= session("UserEmail") %>');

                      else {
                            //$('input[id$=txtState]').val('<%= session("State") %>');

Author Comment

ID: 38312660
So when it reads the session variable address which is Govenour's Place I get the error.

I need a script to clean the value.


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses
Course of the Month19 days, 15 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question