cdxf
cdxf

Reputation: 5648

is there a shorter way for this condition?

if (form.a.value !=""&&form.b.value!="" &&form.c.value !="")

is there a shorter way for this condition?

Upvotes: 0

Views: 137

Answers (3)

Ozzz
Ozzz

Reputation: 300

there are lazy ways :)

if(form.a.value + form.b.value + form.c.value != "" )

if(form.a.value.length + form.b.value.length + form.c.value.length != 0 )

if(!form.a.value && !form.b.value && !form.c.value) 

Upvotes: 1

Alex Reitbort
Alex Reitbort

Reputation: 13706

If you have only three fields(or less), you can leave it as is. If you have more(or unknown) number of fields to check, create an array of fields to check and do the checks in loop in separate function for better maintainability. Something like this:

if(!Empty([form.a,form.b,form.c]))
{
...
}

function Empty(elements)
{
    for(var i=0;i<elements.length;i++)
    {
        if(elements[i].value)
            return false;      
    }
}

Upvotes: 2

Crozin
Crozin

Reputation: 44396

Javascript is weakly-typed so you can treat empty string as boolean false, so the following code should work:

if (form.a.value && form.b.value && form.c.value) {

However I don't know why would you want to change that code. Actually it's quite clear and verbose.

Upvotes: 3

Related Questions