tags:

views:

84

answers:

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

is there a shorter way for this condition?

+3  A: 

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.

Crozin
because it is verbose :), is there shorter way ?
Snoob
Write your code for readability and maintainability. Run it through a minimizer before deployment.
thomasmalt
@patrick dw: Oh, you’re right. Taking a look at the specification revealed that a [string value is only false if the length is zero](http://bclary.com/2004/11/07/#a-9.2).
Gumbo
+2  A: 

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;      
    }
}
Alex Reitbort
thanks for your advice
Snoob
+1  A: 

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) 
Heidi
-1 The first two fail if at least one is not empty.
Gumbo
i'd said them lazy :O
Heidi