'; But...">

Simple comma in ASP.NET session value

I have the following code and it works fine:

var nombreregion = '<%= Session["regionNombre"]%>';

But if the value has a simple comma ('), the following error:

var nombreregion = 'LIBERTADOR GRAL B.O'HIGGINS';

The value is truncated ...

How can I prevent this error?

+4
source share
2 answers

Session["regionNombre"]runs on the server side. So you want to use String.Replace to sanitize the data.

<script type="text/javascript">
    var nombreregion = 
        '<%= ((string)(Session["regionNombre"] ?? "")).Replace("'", "\\'") %>';
</script>
+3
source

You can avoid this by avoiding the single quote ('). For instance:

'LIBERTADOR GRAL B.O\'HIGGINS'

For more information about escaping quotes, you can view the "Special Characters" section on this page:

http://www.w3schools.com/js/js_strings.asp

0
source

Source: https://habr.com/ru/post/1657527/


All Articles