Javascript || Syntax

Posted on 2005-05-03
Last Modified: 2010-04-17
I want a value to be either 0 or 13 digits this is what I have, but it doesn't work.  It returns False everytime

        if (document.Info.MarrsID.value.length != 13 || document.Info.MarrsID.value.length != 0)
       alert("Please Enter a Valid MarrsID.");
        return false;
Question by:mark951
    LVL 53

    Accepted Solution

    One of those has to always be true becasue they are mutually exclusive you want AND, not OR

    if (document.Info.MarrsID.value.length != 13 && document.Info.MarrsID.value.length != 0)
           alert("Please Enter a Valid MarrsID.");
            return false;


    Author Comment

    But a value can not be 13 digits AND 0 Digits at the same time.
    LVL 55

    Assisted Solution

    by:Jaime Olivares
    CobolDinosaur is correct, but if the inverted logic makes more sense to you, you can try:

    if ( ! (document.Info.MarrsID.value.length==13 || document.Info.MarrsID.value.length== 0) )
           alert("Please Enter a Valid MarrsID.");
            return false;

    LVL 10

    Assisted Solution

    by:Andrew Beers
    Here's the reason mark951:

    when you say
    document.Info.MarrsID.value.length != 13 || document.Info.MarrsID.value.lenght != 0
    You are essentially saying this
    If MarrsID.value.length is not 13 or it is not 0 return false
    So the logic states if I have 13 it still is NOT equal to zero and once again returns false.

    The idea you want is if it is 0 or 13 I assume you want to return true so you need && instead of ||.

    Then you would have if MarrsID.value.length is not 13 and it is not 0 then return false
    So if you have 0 or 13 the program will take the else statement and return True.  (I would assume)


    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    find a node in VST 2 37
    pairs challenge 5 33
    wordappend challenge 8 54
    java  and programming certification ? 4 35
    This is an explanation of a simple data model to help parse a JSON feed
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now