1

I have a textarea in a form that i wish to be a completely free test field for the user to add ANY text including url's etc.

the contents of this text area is then going to be passed to a C# webmethod as ajax/json, to be then added to a sql database.

This works great until the users add an apostrophe and/or some other accented chars and i think slashes etc. I know apostrophes are a problems con-tray to most peoples comments on other similar questions as if i remove it the json works.

I have tried simply replace() on the slashes and it works (but im bothered about other chars like \ that also prevent the json working).

What i could do with is a way of filtering (eascaping) anything out that will break the json (' or \ etc) and also deal with a url if its added and also the c# to revert it back from the db on the boud event of the griview

MoiD101
  • 195
  • 1
  • 8
  • 22
  • Take a look at [this SO question](http://stackoverflow.com/questions/1120198/most-efficient-way-to-remove-special-characters-from-string) and [this SO question](http://stackoverflow.com/questions/4503542/check-for-special-characters-in-a-string) – Spaceman Jun 04 '13 at 13:34

3 Answers3

1

You can use JSON2's stringify method:

Example:

<textarea id="txtInput"></textarea>
<div id="submit">Submit</div>

<script src="/Scripts/jquery-1.7.1.js" type="text/javascript"></script>
<script src="/Scripts/json2.min.js" type="text/javascript"></script>
<script>
  $('#submit').click(function () {
    var inputData = $('#txtInput').val();

    var jsonData = {}
    jsonData.input = inputData;
    jsonData.otherStuff = 1;

    var validJson = JSON.stringify(jsonData);

    alert(validJson);
  });
</script>
SBurris
  • 7,378
  • 5
  • 28
  • 36
0

Try these

HttpServerUtility.UrlEncode()
Uri.EscapeUriString()
kostas ch.
  • 1,960
  • 1
  • 17
  • 30
0

encodeURI() and encodeURIComponent()

Konstantin Dinev
  • 34,219
  • 14
  • 75
  • 100
  • I tried the second one, however anomalies seemed to be left in when returning the string back from the database (not sure what to use in the c## databound event for the gridview to properly convert it exactly) plus the encodeURIComponent does not deal with the json effending apostrophes and slashes – MoiD101 Jun 05 '13 at 14:27