Connecting Tech Pros Worldwide Help | Site Map

newline character in string constant

shobhit.mathur@gmail.com
Guest
 
Posts: n/a
#1: Jul 23 '05
Hi!

I have a form with a textarea field. I want to validate the input from
the textarea using javascript. Suppose I want to check that the user
has not entered the string:
"Hello
World!"

To do this I am using the script:
form["text"].value == "Hello\nWorld"

But this gives an "unterminated string constant error" because the
browser converts this to:
form["text"].value == "Hello
World"

So how do I do my check?

Regards,
Shobhit

Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#2: Jul 23 '05

re: newline character in string constant


shobhit.mathur@gmail.com writes:
[color=blue]
> To do this I am using the script:
> form["text"].value == "Hello\nWorld"
>
> But this gives an "unterminated string constant error" because the
> browser converts this to:
> form["text"].value == "Hello
> World"[/color]

No it doesn't, unless you are doing something more that you haven't
shown us. So, give a link to the page where it fails (preferably
a small, self-contained page showing only the problem).

/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Christopher J. Hahn
Guest
 
Posts: n/a
#3: Jul 23 '05

re: newline character in string constant


shobhit.mathur@gmail.com wrote:[color=blue]
> Hi!
>
> I have a form with a textarea field. I want to validate the input from
> the textarea using javascript. Suppose I want to check that the user
> has not entered the string:
> "Hello
> World!"
>
> To do this I am using the script:
> form["text"].value == "Hello\nWorld"
>
> But this gives an "unterminated string constant error" because the
> browser converts this to:
> form["text"].value == "Hello
> World"
>
> So how do I do my check?
>
> Regards,
> Shobhit[/color]

This doesn't happen in pure JavaScript.

I'm going to take a wild guess that this line of code is being output
by PHP, PERL, or some similar server-side tongue. In this case, it is
the server-side language that is interpolating the \n.

If this is the case, depending on the language and the way you're
outputing it, you may be able to double-escape it to \\n.

shobhit.mathur@gmail.com
Guest
 
Posts: n/a
#4: Jul 23 '05

re: newline character in string constant


I am using perl to output the script. Now I am double-escaping it using
\\n. Though the script is output correctly i.e form["text"].value ==
"Hello\nWorld" but when the check still fails.

Shobhit

Closed Thread