Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery check if any of selected inputs is empty

How in a simples way would I test, if any of selected inputs is empty?

I get my inputs this way:

$myinputs = $('.inputstotest');

and would test single input with:

if ( $.trim($this.val()) == '' )

but how to test a group and return true if all are not empty or false if any (at least one) is empty?

like image 816
Marcin Bobowski Avatar asked Jul 19 '13 17:07

Marcin Bobowski


People also ask

How check input field is empty in jQuery?

To check if the input text box is empty using jQuery, you can use the . val() method. It returns the value of a form element and undefined on an empty collection.

How check string is empty in jQuery?

Answer: Use the === Operator You can use the strict equality operator ( === ) to check whether a string is empty or not.


1 Answers

I'd suggest:

var someEmpty = $('.inputstotest').filter(function(){
    return $.trim(this.value).length === 0;
}).length > 0;

With the above someEmpty will be a Boolean, true if there are inputs of that class-name with a trimmed-value equal to zero, false if no inputs of that class-name have a trimmed-value of a length equal to zero.

References:

  • filter().
like image 125
David Thomas Avatar answered Nov 14 '22 22:11

David Thomas