Reputation: 5648
if (form.a.value !=""&&form.b.value!="" &&form.c.value !="")
is there a shorter way for this condition?
Upvotes: 0
Views: 137
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
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
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