118

Is this a good way to check if the value of a field is null?

if($('#person_data[document_type]').value() != 'NULL'){}

Or is there a better way?

Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
tirenweb
  • 30,963
  • 73
  • 183
  • 303

9 Answers9

194

The value of a field can not be null, it's always a string value.

The code will check if the string value is the string "NULL". You want to check if it's an empty string instead:

if ($('#person_data[document_type]').val() != ''){}

or:

if ($('#person_data[document_type]').val().length != 0){}

If you want to check if the element exist at all, you should do that before calling val:

var $d = $('#person_data[document_type]');
if ($d.length != 0) {
  if ($d.val().length != 0 ) {...}
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 34
    `The value of a field can not be null, it's always a string value.` this isn't entirely true. I've got a `select` containing no `options`. When I perform `.val()` on this it returns `null`. Jquery 1.7.2 – Liam Jan 14 '13 at 11:07
  • @Pispirulito: I don't think so, and I don't think that it will be changed. A `select` without any `option` elements is not something that you normally have. It's pretty useless. – Guffa Jan 30 '14 at 21:22
  • 1
    @Guffa beg to differ, an empty ` – Malcolm Salvador Aug 01 '17 at 03:18
  • If you use the following solution to provide a placeholder in your `select`, it will start out with a `null` value because a 'disabled' option is selected. https://stackoverflow.com/questions/5805059/how-do-i-make-a-placeholder-for-a-select-box – Sygmoral May 04 '19 at 01:51
  • This worked for me not equal null `if ($('#person_data[document_type]').val() !== null){}` – SWAT 10101 Nov 26 '21 at 17:40
38

I would also trim the input field, cause a space could make it look like filled

if ($.trim($('#person_data[document_type]').val()) != '')
{

}
Flipke
  • 971
  • 6
  • 10
17

Assuming

var val = $('#person_data[document_type]').value();

you have these cases:

val === 'NULL';  // actual value is a string with content "NULL"
val === '';      // actual value is an empty string
val === null;    // actual value is null (absence of any value)

So, use what you need.

darioo
  • 46,442
  • 10
  • 75
  • 103
15

that depends on what kind of information are you passing to the conditional..

sometimes your result will be null or undefined or '' or 0, for my simple validation i use this if.

( $('#id').val() == '0' || $('#id').val() == '' || $('#id').val() == 'undefined' || $('#id').val() == null )

NOTE: null != 'null'

Wirone
  • 3,304
  • 1
  • 29
  • 48
8
_helpers: {
        //Check is string null or empty
        isStringNullOrEmpty: function (val) {
            switch (val) {
                case "":
                case 0:
                case "0":
                case null:
                case false:
                case undefined:
                case typeof this === 'undefined':
                    return true;
                default: return false;
            }
        },

        //Check is string null or whitespace
        isStringNullOrWhiteSpace: function (val) {
            return this.isStringNullOrEmpty(val) || val.replace(/\s/g, "") === '';
        },

        //If string is null or empty then return Null or else original value
        nullIfStringNullOrEmpty: function (val) {
            if (this.isStringNullOrEmpty(val)) {
                return null;
            }
            return val;
        }
    },

Utilize this helpers to achieve that.

Nilesh Moradiya
  • 691
  • 1
  • 10
  • 19
0

jquery provides val() function and not value(). You can check empty string using jquery

if($('#person_data[document_type]').val() != ''){}
Chinmayee G
  • 7,947
  • 2
  • 31
  • 41
0

Try

if( this["person_data[document_type]"].value != '') { 
  console.log('not empty');
}
<input id="person_data[document_type]" value="test" />
Kamil Kiełczewski
  • 85,173
  • 29
  • 368
  • 345
0

My Working solution is

var town_code = $('#town_code').val(); 
 if(town_code.trim().length == 0){
    var town_code = 0;
 }
Anes
  • 35
  • 1
  • 9
0

In your code 'NULL' is a string. So its wrong.

You can use exclamation mark ! to check if it is null.

Here is the code:

if(!$('#person_data[document_type]').val()){

}

The above code means that if the $('#person_data[document_type]') has no value (if the value is null).

MB_18
  • 1,620
  • 23
  • 37