how to check for apostrophe in a string

Posted on 2012-08-20
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

    Just use .replace method

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

    Expert Comment

    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

    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

    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

    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.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Live - One-on-One JavaScript Help from Top Experts

    Solve your toughest problems, fast.
    JavaScript experts are online now and ready to help you.

    Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    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…

    737 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

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now