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?
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.
Answer: Use the === Operator You can use the strict equality operator ( === ) to check whether a string is empty or not.
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()
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With