jQuery Check Field Null or Empty (JCFNE)

Is this a good way to check if a field value is null? is it like this?

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

Or is there a better way?

Solutip

The field value cannot be null, it is always a string value.

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

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

or:

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

If you want to check whether the element exists, you must do so before calling val:

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

Post a Comment

Previous Next

نموذج الاتصال