Kavin Mehta
Kavin Mehta

Reputation: 810

check multiple values in an if statement in jquery/javascript

I am trying to if the value of an input box is blank or not by doing the below:

var id = $(this).attr('id').substr(3);
var lengthname = $("#input_name_"+id).val().length;
var lengthrss = $("#input_rss_"+id).val().length;
if (lengthrss!=0 || lengthname!=0)
{
  // do something
}
else
{
alert('Values cannot be blank');
}

For some reason it is not doing the OR however if the user enters both values as blanks then the alert comes up??

Upvotes: 0

Views: 6678

Answers (1)

Wladimir Palant
Wladimir Palant

Reputation: 57651

What you are looking for is an AND operation, not OR. The valid case is when both lengths are non-zero:

if (lengthrss!=0 && lengthname!=0)
{
  // do something
}

Alternatively:

if (lengthrss==0 || lengthname==0)
{
  alert('Values cannot be blank');
}

For reference: De Morgan's laws

Upvotes: 3

Related Questions