check if object items are empty

Asked

Viewed 244 times

0

Hello, I have an object that receives parameters from a form and send this object to another page. On this page I need to do a validation if at least three items have been completed. Could someone help me?

1 answer

3


You can browse the values of the object using for..in and save to a counter. If the counter is greater than 2, it means that at least 3 values have been filled in. Example:

var obj = {
    "param1": "1",
    "param2": "2",
    "param3": "",
    "param4": "",
    "param5": ""
  },
  cont = 0;

for (propriedade in obj) {
  // retorna true se o valor for diferente de undefined, vazio, null, zero, espaços
  if (obj[propriedade])
    cont++;
}

if (cont > 2)
  alert("Pelo menos 3 preenchidos")
else
  alert("Falta à preencher");

  • 1

    Thanks man, it worked out, thanks...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.