0

I am working with editable grid in my application. I have columns which vary from a string datatype to number, I am facing some problem While validating for emptiness of a grid cells. As easy as it sounds, it is getting confusing and frustrating.

This is what I am using to check if the field is empty:

if (value == "") {
//do work here
}

This works fine for columns with string datatype, but when a value of datatype number is there, it fails to check it as value contains NaN at that moment. I have tried most methods mentioned in this answer, but in vain. Secondly, I also want to avoid nested or unnecessary ifs in this case..

Community
  • 1
  • 1
faizanjehangir
  • 2,771
  • 6
  • 45
  • 83

2 Answers2

3

You could do a check for a falsy values by just checking for

if (!value) {
    //do work here
}

Which would evaluate to true for 0,"",null,undefined,false and NaN

or of course if you want to test if the field is not empty you can just check for a truthy value

if (value) {
    //do work here
}

Which evaluates to true if the value is not falsy (none of the above)

Moritz Roessler
  • 8,542
  • 26
  • 51
  • Yes! It works now, though `if (value){...}` worked for me..thanks – faizanjehangir Mar 21 '13 at 07:58
  • You're welcome. Oh, so you want to check for a `truthy` value (if the field is not empty), it's the same idea though, `if (value){...}` will evaluate to true if the value is none of the above mentioned – Moritz Roessler Mar 21 '13 at 08:01
1

You can check for truthy value

if( value ) {
}

which evaluates to true is it is not from ('null', 'undefined', 'false', 'NaN' or 'empty' that is ("") )

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105