Counting the number of empty inputs with a specific class

I tried a couple of solutions from previous questions, but no luck. Hope someone can help me.

I have a form section where fields are dynamically created with a specific class:

<td><input class="user_field" type="text" name="1[user_fname]"/></td> <td><input class="user_field" type="text" name="1[user_lname]"/></td> <td><input class="user_field phone" type="text" name="1[user_mobile]"/></td> <td><input class="user_field" type="text" name="1[user_email]"/></td> <td>&nbsp;</td> 

When blurring, I need to check for empty boxes and tried:

 $('.user_field').blur(function(){ //check all fields for complete alert ($('.user_field[value=""]').length) }); 

and get "0"

+6
source share
3 answers

This will give you all the empty inputs:

 $('.user_field').filter(function(){ return !$(this).val(); }).length; 
+17
source

mm just submit my version using .not

 $('.user_field').blur(function() { var count = $('.user_field').not(function() { return this.value; }).length; alert(count); }); 

Demo

+1
source
 $('.user_field').blur(function(){ alert ($('.user_field').filter('[value=""]').length); }); 

Demo

0
source

Source: https://habr.com/ru/post/915112/


All Articles