...

View Full Version : && Validation



adamdressler
10-03-2011, 06:31 AM
I keep getting validation errors for using && as and.

I've read to put "&" in place of it, but whenever I do this, the code stops working.

I've tried seperate parenths, and still confused.

anyone help to make this work and validate at the same time?



if (first=="" && last=="")
first = last = "Unknown";


if (first == '')
document.writeln(last);
else if (last == '')
document.writeln(first);
else
document.writeln (last + ", " + first);

Philip M
10-03-2011, 07:27 AM
There is nothing wrong with your script -what are the "validation errors" you mention? &amp is HTML, not Javascript.



<script type = "text/javascript">
var first = "";
var last = "";

if (first=="" && last=="")
first = last = "Unknown";
alert (first + " " + last);

if (first == '')
document.writeln(last);
else if (last == '')
document.writeln(first);
else
document.writeln (last + ", " + first);

</script>

Remember that document.write erases the page and creates a new one.

It is always recommended to use braces { } around conditional code.


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

venegal
10-03-2011, 01:48 PM
There is something wrong, actually: Since it's an inline script (i.e. your code is put directly into the HTML page, and not in its own .js file), you can't use & or > that way. You are using an XHTML doctype (HTML would be ok with &), and so, in order for those ampersands to not be interpreted as the start of an entity, you have to wrap your code in CDATA delimiters, like this:


<script type="text/javascript">
/* <![CDATA[ */

if (first=="" && last=="") {} // This is fine

/* ]]> */
</script>

Philip M
10-03-2011, 04:54 PM
You are using an XHTML doctype (HTML would be ok with &),

Ah, I had not noticed that! :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum