John Doe
John Doe

Reputation: 51

C# and JavaScript - Escaping Single Quote Character

I have an XML file storing a string like the following:

Error: The form's name field was left empty!

Now, this message in the XML file is used in various locations. For instance, it is sometimes bound to an aspx control and sometimes displayed in a JavaScript alert.

In order to escape the single quote character, I modified the message as follows:

Error: The form\'s name field was left empty!

When the message is shown in a JavaScript alert, it is displayed as it should with the single quote escaped. However, when it is bound to an aspx control, it is displayed as is.

I tried modifying the message as follows:

Error: The form's name field was left empty!

and

Error: The form‘s name field was left empty!

With these two cases, the message displays as it should when bound to an asp control. However, when passed to a JavaScript alert, I get the ") expected" error in IE8.

How can I get this to work for both aspx controls and JavaScript alerts please? I have already wasted a lot of time on it. Thank you so much.

N.B. I am using a very old version of the .NET Framework, specifically 2.0.

Upvotes: 0

Views: 475

Answers (1)

Belogix
Belogix

Reputation: 8147

You should just store your messages verbatim and then let each client handle the special character. So, store as Error: The form's name field was left empty! and then within each language add a step that checks for special characters and escapes them accordingly.

Upvotes: 2

Related Questions