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

x
?
Solved

how to check for apostrophe in a string

Posted on 2012-08-20
5
Medium Priority
?
1,065 Views
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.

thanks
0
Comment
Question by:mgmhicks
5 Comments
 
LVL 83

Accepted Solution

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

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

Expert Comment

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

http://php.net/manual/en/function.addslashes.php

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()
http://php.net/manual/en/function.json-encode.php

And decode in Javascript using jQuery's parseJSON() function
http://api.jquery.com/jQuery.parseJSON/
0
 
LVL 16

Expert Comment

by:grahamnonweiler
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.
0
 

Author Comment

by:mgmhicks
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$=txtFName]').val('');
                            $('input[id$=txtLName]').val('');
                            $('input[id$=txtAddr1]').val('');
                            $('input[id$=txtCity]').val('');
                            //$('input[id$=txtState]').val('<%= session("State") %>');
                            $('input[id$=txtZipCode]').val('');
                            $('input[id$=txtEmail]').val('');
                      }
                };
0
 

Author Comment

by:mgmhicks
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.

thanks
0

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