Javascript Question

Javascript: How to properly check if array is empty

I am trying to validate my array input fields if its empty, unfortunately my code seems not working i don't know why, and what is the best way to validate this.

<input type="text" name="sub_crit_text[]" class="sub_crit_text" value="text1">
<input type="text" name="sub_crit_text[]" class="sub_crit_text" value="text2">
<input type="text" name="sub_crit_text[]" class="sub_crit_text" value="text3">
<input type="text" name="sub_crit_text[]" class="sub_crit_text" value="text4">


My js

var sub_crit_arr = [];
$('.sub_crit_text').each(function(k , v){
sub_crit_arr[k] = $(v).val();
});

if(sub_crit_arr.length > 0){

}else{
alert('Sub criteria cannot be empty');
return false;
}

Answer

You can do it with filter alone,

if($('.sub_crit_text').filter(function(){ return !this.value.trim(); }).length){
    alert('Sub criteria cannot be empty');
    return false;
}

There is no need to introduce another one array and also you can cut down one more iteration.