How to make a multiline alert?

T

Ted Byers

Here is the beginning of the function:
print "function validateFormData(vtf2) {
var x = vtf2.ccn.value
var msg = \"\";
var newline = \'CR\';
if (x==null || x==\"\") {
vtf2.ccn.bgColor=\"lightred\";
msg.concat(\"\\r\\n\",\"Credit card number must be
provided\");
}
if (!isNumber(x)) {
msg.concat(\"\\r\\n\",\"Credit card number must be a
number\");
}
x = vtf2.ccexp.value
if (x==null || x==\"\") {
msg.concat(\"\\r\\n\",\"Credit card expiry date must be
provided\");
alert(msg);
return false;
}

I managed to get this function to be executed, but it only displays
the first "Credit card number must be provided", but not the second
"Credit card expiry date must be provided'. The concatenation looks
like it is working, at least the first time. But what can be the
problem. Is there a limit t the number of times concat can be called
on a string, or is alert limited in terms of the number of lines of
text it can display?

Any insights that can be provided would be appreciated.

Thanks

Ted
 
J

Jukka K. Korpela

Here is the beginning of the function:
print "function validateFormData(vtf2) {

With all due respect (I'm a lifestyle Perlert my self), please note that
this is a JavaScript group and people here should not be expected to
know languages like Perl or to be willing to run a Perl interpreter just
to see the actual JavaScript code generated. Moreover, newsreaders tend
to break lines and other things.

So please post the generated JavaScript code or, much more preferably,
the URL of a page that demonstrates the problem in context.
msg.concat(\"\\r\\n\",\"Credit card number must be
provided\");

The invocation msg.concat(...) returns a concatenated string (consisting
of the value of msg and the argument values). It does not change the
value of msg, as the above seems to assume.
I managed to get this function to be executed, but it only displays
the first "Credit card number must be provided",

That's strange, because it seems that in your code, the value of msg is
initialized to the empty string and not changed then at all.

To modify the current value of msg by appending the values of a and b to
it, you could use

msg = msg.concat(a, b)

though I guess people mostly use just

msg += a + b
 
G

Gene Wirchenko

On Tue, 20 Dec 2011 15:09:06 -0800 (PST), Ted Byers

[snip]
Any insights that can be provided would be appreciated.

alert("Hello\nworld!");
works for me.

I think you can remove the "\r"s.

You might have an error in your escaping, but it is hard to tell.

Sincerely,

Gene Wirchenko
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top