Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: && Validation

  1. #1
    New Coder
    Join Date
    Sep 2011
    Posts
    48
    Thanks
    1
    Thanked 0 Times in 0 Posts

    && Validation

    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);

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,151
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    There is nothing wrong with your script -what are the "validation errors" you mention? &amp is HTML, not Javascript.


    Code:
    <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.
    Last edited by Philip M; 10-03-2011 at 07:31 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    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:

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

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

     /* ]]> */ 
    </script> 

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,151
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    Quote Originally Posted by venegal View Post
    You are using an XHTML doctype (HTML would be ok with &),
    Ah, I had not noticed that!

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •