How do I escape form input values, C style?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ivan Marsh

    #1

    How do I escape form input values, C style?

    Hey Folks,

    How do I go about escaping values entered into a form so they can be
    dumped into a database?

    For the record, I have no idea how the functions I'm using to write to the
    database work because it's a proprietary product that I have to use, and
    "they" don't release any documentation about how their internals work. I
    just need to be able to prepare the string before it's passed to their
    function.

    For example, the classic last name = O'Maley... and I can't use escape(),
    I need it to come out O\'Maley.

    A single quote isn't the only thing I'm concerned with but it's a start,
    and solves my immediate problem.

    Is there an all powerful function that escapes all special
    (non-alphanumeric) characters or do I need some fancy regex?

    Any help is, as always, appreciated.

    --
    Life is short, but wide. -KV

  • Fred Oz

    #2
    Re: How do I escape form input values, C style?

    Ivan Marsh wrote:[color=blue]
    > Hey Folks,
    >
    > How do I go about escaping values entered into a form so they can be
    > dumped into a database?[/color]

    Values entered into forms are 'escaped' automatically by the browser
    before submission to the server. O'Malley will appear in the URI sent
    to the server as:

    O%27Malley

    Of course, if you are talking about un-escaping at the server, that
    has nothing to do with client-side JavaScript.

    [...]


    --
    Fred

    Comment

    • Ivan Marsh

      #3
      Re: How do I escape form input values, C style?

      On Thu, 14 Apr 2005 01:24:37 +1000, Fred Oz wrote:
      [color=blue]
      > Ivan Marsh wrote:[color=green]
      >> Hey Folks,
      >>
      >> How do I go about escaping values entered into a form so they can be
      >> dumped into a database?[/color]
      >
      > Values entered into forms are 'escaped' automatically by the browser
      > before submission to the server. O'Malley will appear in the URI sent
      > to the server as:
      >
      > O%27Malley
      >
      > Of course, if you are talking about un-escaping at the server, that
      > has nothing to do with client-side JavaScript.[/color]

      I guess I should include an example... this data is being grabbed and
      written run-time, and I don't think a "true" form submission is being
      performed.

      Here's some pseudo-code:

      <script type="text/javascript">

      query_string = "UPDATE mytable SET myfield = '" +
      ProprietaryFiel dGrab("Last_Nam e") + "' WHERE otherfield = '" +
      ProprietaryFiel dGrab("Phonenum ") + "';";

      ProprietaryData baseWrite(query _string, databasename);

      </script>

      Last_Name and Phonenum are fields in the form.

      I need to do a C-like escape in query_string whenever
      ProprietaryFiel dGrab() is called.

      Oh... and the proprietary functions are not in my control or editable by
      me.

      Any takers?

      --
      Life is short, but wide. -KV

      Comment

      • fox

        #4
        Re: How do I escape form input values, C style?



        Ivan Marsh wrote:[color=blue]
        > On Thu, 14 Apr 2005 01:24:37 +1000, Fred Oz wrote:
        >
        >[color=green]
        >>Ivan Marsh wrote:
        >>[color=darkred]
        >>>Hey Folks,
        >>>
        >>>How do I go about escaping values entered into a form so they can be
        >>>dumped into a database?[/color]
        >>
        >> Values entered into forms are 'escaped' automatically by the browser
        >> before submission to the server. O'Malley will appear in the URI sent
        >> to the server as:
        >>
        >> O%27Malley
        >>
        >> Of course, if you are talking about un-escaping at the server, that
        >> has nothing to do with client-side JavaScript.[/color]
        >
        >
        > I guess I should include an example... this data is being grabbed and
        > written run-time, and I don't think a "true" form submission is being
        > performed.
        >
        > Here's some pseudo-code:
        >
        > <script type="text/javascript">
        >
        > query_string = "UPDATE mytable SET myfield = '" +
        > ProprietaryFiel dGrab("Last_Nam e") + "' WHERE otherfield = '" +
        > ProprietaryFiel dGrab("Phonenum ") + "';";
        >
        > ProprietaryData baseWrite(query _string, databasename);
        >
        > </script>
        >
        > Last_Name and Phonenum are fields in the form.
        >
        > I need to do a C-like escape in query_string whenever
        > ProprietaryFiel dGrab() is called.
        >
        > Oh... and the proprietary functions are not in my control or editable by
        > me.
        >
        > Any takers?
        >[/color]

        query.replace(/([\'\"])/g,"\\$1"); ?

        Comment

        Working...