Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if inputs form are empty jQuery

How can I check in jQuery if inputs with name denominationcomune_{{loop.index}} and denominationcomune_{{loop.index}} are empty not all inputs ?

I have a twig like this:

<form action="" method="post">
    {% for mat in mat_temp_array %}
        <input type="text" name="nomentite_{{loop.index}}"/>
        <input type="text" name="denominationcomune_{{loop.index}}" value="{{ mat.denominationcommun }}"/>
        <input type="text" name="denominationcomerce_{{loop.index}}" value="{{ mat.denominationcomerce }}"/>
    {% endfor %}

     <input type="submit" class="btn" value="save"/>
</form>
like image 862
user2721553 Avatar asked Aug 27 '13 12:08

user2721553


2 Answers

var empty = true;
$('input[type="text"]').each(function() {
   if ($(this).val() != "") {
      empty = false;
      return false;
   }
});

This should look all the input and set the empty var to false, if at least one is not empty.

EDIT:

To match the OP edit request, this can be used to filter input based on name substring.

$('input[name*="denominationcomune_"]').each(...

like image 133
nubinub Avatar answered Oct 11 '22 00:10

nubinub


You could do it like this :

bool areFieldEmpty = YES;
//Label to leave the loops
outer_loop;

//For each input (except of submit) in your form
$('form input[type!=submit]').each(function(){
   //If the field's empty
   if($(this).val() != '')
   {
      //Mark it
      areFieldEmpty = NO;
      //Then leave all the loops
      break outer_loop;
   }
});

//Then test your bool 
like image 35
Bigood Avatar answered Oct 11 '22 01:10

Bigood