Javascript || Syntax

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;
        }
mark951Asked:
Who is Participating?
 
COBOLdinosaurCommented:
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;
        }


Cd&
0
 
mark951Author Commented:
But a value can not be 13 digits AND 0 Digits at the same time.
0
 
Jaime OlivaresSoftware ArchitectCommented:
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;
        }

0
 
Andrew BeersTechnology LeadCommented:
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)


~Aqua
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.