Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 968801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:32:30+00:00 2026-05-16T02:32:30+00:00

I need to create a string in javascript from some text that the user

  • 0

I need to create a string in javascript from some text that the user provides. When I try to run this line:

var str = new String('<%= @note.text %>')

I get the unterminated string literal error. Given that I have no control over what is inputed, and I can’t assign the string to start to do any character replacement, how do I fix this problem? Thanks for reading.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:32:31+00:00Added an answer on May 16, 2026 at 2:32 am

    There’s nothing you can do client-side, you need to sanitize the string on the server. Given that you’re putting the literal into the String constructor via <%= %>, I assume you’re using some variety of ASP.Net.

    I’m sure there’s a more elegant way to do this, but this should work as a first pass at encoding a string for use in JavaScript. This makes no attempt at addressing the relative merits of passing arbitrary strings to JavaScript in the first place. (For most cases, there should very likely be some server-side checks for malicious strings.)

    Assuming note is a text input field, something like this might work….

    // New Property in your code behind
    public string outputText {get; private set;}
    

    In the OnLoad(), add

    // Encode the string
    string tempText = Note.Text
    outputText = String.empty;
    foreach( char character in tempText)
    {
      // Prefix quotation mark with a backslash,
      if(char == "\"")
        outputText += "\\\"";
      // Prefix apostrophe with a backslash,
      else if(char == "'")
        outputText += "\\'";
      // convert newline to a literal.
      else if(char == "\n")
        outputText += "\\n";
      else
        outputText += character;
    }
    

    And finally, in your .aspx

    var str = new String('<%= outputText %>')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 496k
  • Answers 496k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Knuth is not the place to start. It's the place… May 16, 2026 at 11:32 am
  • Editorial Team
    Editorial Team added an answer The two options are not equivalent. In option 1 there… May 16, 2026 at 11:32 am
  • Editorial Team
    Editorial Team added an answer ^[^<>]*> if you need the corresponding < as well, ^[^<>]*>[^<]*<… May 16, 2026 at 11:32 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

i need some help understanding how i can create a new custom event. i
Now I'm working on a web project. I need to create a XML document
I understand that JavaScript isn't really made to open and save files. The best
I need populate with dummy values a table. I need create a random generated
I have code written that converts my Document to a string prior to printing
What I am trying to do is create a function that will be called
Say I have the string 1,2,3,4,5 and I want to convert this to an
I need to find a way to call a vb.net function in my aspx
I'm trying to test web service calls using an ASP.NET page that creates a
this is my first visit to stackoverflow and right now I feel very comfortable

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.